bottom scroll is not working in table view - objective-c

I am having a table view. in that I am displaying an array. when I scroll the tableview its not moving up to the bottom of a last table cell. what should I do for that.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return tableResourceArray.count;}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cellIdentifier=[NSString stringWithFormat:#"CustomCell:%d",indexPath.row];
customcell *cell = (customcell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[customcell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
if(tableView==table) {
table.backgroundColor = [UIColor clearColor];
table.separatorColor=[UIColor clearColor];
NSDictionary *slug = [category objectAtIndex:indexPath.row];
NSLog(#"gggg:%#",slug);
cell.ctitle.text = [NSString stringWithFormat:#"%#", [slug objectForKey:#"title"]];
cell.ctitle.font=[UIFont fontWithName:#"Arial" size:12];
cell.cdate.text = [NSString stringWithFormat:#"Date:%#", [slug objectForKey:#"date"]];
cell.cdate.textColor=[UIColor colorWithRed:0.820 green:0.776 blue:0.526 alpha:1.000];
cell.ccontent.text = [NSString stringWithFormat:#"%#", [slug objectForKey:#"content"]];
}

Solution 1 :
You have declared Identifier as non-static variable. This might cause scroll issue.
NSString *cellIdentifier=[NSString stringWithFormat:#"CustomCell:%d",indexPath.row];
Instead use below :
NSString *cellIdentifier= #"CustomCellIdentifier";
Static variable are used to construct only once and it avoid memory creation all the time whenever “CellForRowAtIndexPath” method is called. If non-static variable is used lots of memory are created that may lead to scrolling issue.
Solution 2 : // Not sure. But might help
Allocate dictionary before usage.
NSDictionary *slug = [[NSDictionary alloc] init];
slug = [category objectAtIndex:indexPath.row];

I solved the problem by changing my table view frame.

Assuming that your screen size is 320x480, the bottom part of your table view frame is out of screen by 130 because of your table view's frame (origin y 130 + height 480). Or even more off if you have more subviews at the bottom of screen that cover tableview. Change your table view frame so that the bottom of your table view ends at the bottom of the screen.

Related

TableViewCell not displaying layout

Here's a question about tableViewCell. I am trying my hand at table views for the first time and trying to get my head around reusable cells etc. I have managed to get it working to some extent. I have a tableView that lives on a child view controller which is its delegate and data source. Code for delegate:
#import "ChildViewController.h"
#interface ChildViewController ()
#property NSArray *titles;
#property NSArray *thumblenails;
#end
#implementation ChildViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.titles = [NSArray arrayWithObjects:#"Title 1", #"Title 2", #"Title 3", #"Title 4",nil];
self.thumblenails = [NSArray arrayWithObjects:#"Image1", #"Image2", #"Image3", #"Image4", nil];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.titles.count;
}
-(UITableViewCell *)tableView:(UITableView *)TableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
SimpleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SimpleTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
// cell.textLabel.backgroundColor = [UIColor greenColor];
cell.textLabel.text = [self.titles objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:[self.thumblenails objectAtIndex:indexPath.row]];
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I also have a SimpleTableViewCell.xib:
I have set the class on the identity inspector to SimpleTableViewCell and imported the SimpleTableViewCell.h file to my ChildViewController.h. I have also set the identifier to "cell" in the attributes inspector of the Table view cell, but here is the thing. As I am not getting the look I want for my cell, I have tried misspelling it and nothing has changed so clearly the identifier is not doing anything. When I run my app I get this:
So the array of images and titles are being loaded but not the actual cell size and labels that I set on the xib file for the cell. I have tried changing something in the cell, like the background color in the attributes inspector.
and on the xib it looks like this:
but when I run it:
I am guessing all this is because the cell identifier is not actually linked, but I thought I had done that in the line:
SimpleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
Am I missing some important coding in here, linking something in the xib, or is it just a typo? any suggestions greatly appreciated.
//////////////////////////////////////////////////////////////////////UPDATE1////////////////////////////////////////////////////////////////////////
I have tried the second suggestion and the result is this:
So, hopefully this will throw some light into what I am doing wrong?
Also, I have corrected the original text, when I change the color to green it's using the attributes inspector.
Thanks for your help.
//////////////////////////////////////////////////////////////////////UPDATE2////////////////////////////////////////////////////////////////////////
I have removed the lines:
cell.textLabel.text = [self.titles objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:[self.thumblenails objectAtIndex:indexPath.row]];
Also, added the code:
[self.tableView registerNib:[UINib nibWithNibName:#"SimpleTableCell" bundle:nil] forCellReuseIdentifier:#"cell"];
and the code:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return [UIScreen mainScreen].bounds.size.width * (0.3);
}
Now my table looks like this:
So question now is how do I add the actual values of the original arrays of titles and images?
you should set the height of cell to get the exact same look in the nib file. use this delegate method to set height of cell.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [UIScreen mainScreen].bounds.size.width * (0.3);// 0.3 should be your aspect ratio for cell:
// for cell.height/ cell.width in nib file.
}
You forgot to register nib for cell identifier.
Put [self.tableView registerNib:[UINib nibWithNibName:#"SimpleTableViewCell" bundle:nil] forCellReuseIdentifier:#"cell"]; to viewDidLoad method.
And you can remove
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SimpleTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}

UITableView imageView Shows Image Where An Image Should Not Be After Scrolling

I have a uitableview with a list of people in it. some records have an image and some records do not. If i scroll down through the list it appears correct but if i scroll back up then an image of another person starts to show on other people's cell row where an image should not be. Here is my code for cellForRowAtIndexPath
// START CELL LABELLING FOR TABLE VIEW LIST //
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
Person *person = [arrayOfPersons objectAtIndex:indexPath.row];
NSString *personPhoto = person.personPhoto;
NSString* imgPath = [docPath stringByAppendingPathComponent:
[NSString stringWithFormat:#"%#", personPhoto] ];
if ([[NSFileManager defaultManager] fileExistsAtPath:imgPath]){
NSLog(#"FILE EXISTS");
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(240, 0, 67, 67)];
imageView.image = [UIImage imageWithContentsOfFile:imgPath];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[cell.contentView addSubview:imageView];
}else{
NSLog(#"Does Not Exist");
}
cell.textLabel.text = person.personName;
return cell;
imageView = nil;
personPhoto = #"";
imgPath = #"";
}
// END CELL LABELLING FOR TABLE VIEW LIST //
The reason this is happening is because table cells get re-used.
When you use [tableView dequeueReusableCellWithIdentifier:CellIdentifier], you get back a cell that has already been shown in your table view (for a different index path). You may have already added an image view to this cell for the Person at this previous index path, and nowhere do you remove this image.
Therefore, when the current Person doesn't have a photo, the previous image will remain visible in the cell.
I would recommend creating your own UITableViewCell subclass and adding a UIImageView to it, so that you can easily get a reference back to the image view (or you could use a view tag, if you prefer).
Either way, you need to remove the image view/set the image to nil when the person does not have a photo.

why UITableView is showing data when I touch it?

I have a UITableView loading some tweets, all works nicely but when I populate the table it doesn't enter to the CellForRow method til I touch the table view.... does somebody know how to solve this?
tab = [[UITableView alloc] initWithFrame:CGRectMake(10, 300, 400, 200) style:UITableViewStylePlain];
tab.dataSource = self;
tab.delegate = self;
then...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"entra a cellula");
Tweet *t = [tweets objectAtIndex:indexPath.row];
static NSString *MyIdentifier = #"MyIdentifier";
// Try to retrieve from the table view a now-unused cell with the given identifier.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
// If no cell is available, create a new one using the given identifier.
if (cell == nil) {
// Use the default cell style.
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:MyIdentifier];
}
cell.textLabel.text = t.screenName;
cell.detailTextLabel.text = t.text;
cell.imageView.image = t.profileImage;
return cell;
}
Best guess based on what we know is that you aren't adding the table as a subview on the main thread. This means that redrawing doesn't occur until you scroll. Try wrapping the addsubview call in a GCD call like so:
dispatch_async(dispatch_get_main_queue(), ^() {
[self addSubview:tab];
});
When you finished loading your tweets call this method:
[tab reloadData];
to reload your tableView data source.

Reloading searchResultsTableView

I have, in my main tableView, some custom cells (cells with an imageView, basically).
The imageView is set only if a value on my plist is false. Then, when it's true, the imageView is nil.
Basically when the user enters the detailView, the value is set to YES and the cell.imageView is nil.
And it's okay, it works
I'm using a searchDisplayController, when i search for something that has a cell.imageView, going into the detailView and then coming back to the searchResultsTable, the cell has still the image, while it shouldn't, but the main tableView has the correct cell (so, with no image).
I thought that it could depend on searchResultsTableView, but i'm not sure.
I tried with
[self.searchDisplayController.searchResultsTableView reloadData];
with no effect.
How could i reload the searchResultsTableView so that it shows the right cells, those with the image and those that don't have the image anymore?
Any help appreciated!
EDIT
This is my cellForRowAtIndexPath method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSArray *rows;
if (tableView == self.searchDisplayController.searchResultsTableView) {
rows = filteredList; //for search
} else {
NSDictionary *section = [localSortedTips objectAtIndex:indexPath.section];
rows = [section objectForKey:#"Rows"];
}
NSDictionary *item = [rows objectAtIndex:indexPath.row];
cell.textLabel.text = [item objectForKey:#"name"];
if ([[item valueForKey:#"isRead"] boolValue] == NO) {
cell.imageView.image = [UIImage imageNamed:#"unread.png"];
} else {
cell.imageView.image = nil;
}
cell.textLabel.font = [UIFont boldSystemFontOfSize:15.0];
cell.textLabel.adjustsFontSizeToFitWidth = YES;
return cell;
}
If I understood you right, then you can have a workaround but searching again with the same search string:
if (self.searchDisplayController.active) {
self.searchDisplayController.searchBar.text = self.searchDisplayController.searchBar.text;
}
put it in viewWillAppear: or viewDidAppear: which will be called each time the view is shown up (eg. you go back from the detail view to your search view). And reloading the data in this place would be nice too, to get the right data (for example if you marked the cell as read like in your sample code)
Just [self.tableView reloadData]; and not the searchResultsTableView (it will be automatically use the updated data after the new search)
It sounds as if perhaps your cells are being recycled and not reset properly. UITableView uses view recycling, so it's important that if you do something such as set an image you make sure it is explicitly set even when their isn't an image to display.
If you share your cellsForRowAtIndexPath code you might be able to get some more help.

Animating a font size change in a UITableView

Hey Simple little app for educational purposes, its a simple uitableview and I want users to be able to use a uislider to adjust the font size as needed. The code I have now works to change the font but only when the view is updated, ie when i pull up or down the table view to reveal other cells. I'd like the font change to be reflected immediately as a user moves the uislider if possible, here's the code that I have working half way:
-(UITableViewCell *) tableView: (UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath {
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if (cell == nil)
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier: SimpleTableIdentifier] autorelease];
NSUInteger row = [indexPath row];
cell.text = [listData objectAtIndex:row];
UIImage *image = [UIImage imageNamed:#"star.png"];
cell.font = [UIFont systemFontOfSize:[fontSlider value]];
cell.image = image;
return cell;
}
You could make an IBAction for the fontSlider for the "Value Changed" option as follows:
-(IBAction) refreshResize
{
[self.view setNeedsDisplay];
}
That should work. setNeedsDisplay refreshes the screen.