UITableView repeating cells when scrolled - iphone

When i scroll down my UITableView, it starts showing me the same cells that i've already seen, and scrolling around a bit continues to put cells in the wrong place.
Here's the code i'm using. If anything additional is needed then let me know:
.h
#interface HomeViewController : UITableViewController {
int numberOfRows;
NSArray *allVaults;
}
#property (nonatomic, assign) int numberOfRows;
#property (nonatomic, retain) NSArray *allVaults;
#end
.m
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSString *vaultsPath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/Vaults"];
NSFileManager *fileManager = [NSFileManager defaultManager];
self.allVaults = [fileManager contentsOfDirectoryAtPath:vaultsPath error:nil];
numberOfRows = [self.allVaults count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return numberOfRows;
}
- (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];
NSString *vaultsPath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/Vaults"];
NSString *dictionaryPath = [NSString stringWithFormat:#"%#/%#",
vaultsPath,
[self.allVaults objectAtIndex:indexPath.row]];
NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:dictionaryPath];
cell.backgroundView = [AHCellCreation backgroundView];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.selectedBackgroundView = [AHCellCreation selectedBackgroundView];
cell = [AHCellCreation createCellWithDictionary:dictionary Cell:cell];
}
return cell;
}
Any help is appreciated!
EDIT 1: Image to show what happens when i move most code outside the (cell == nil) if statement:
Before:
After:
EDIT 2:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 82;
}

It looks as if you are only setting the cell content when the you're getting a nil back from dequeueReusableCellWithIdentifier. You need to set the cell contents every time, not just when you need to create a new cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
AHCell *cell = (AHCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
// create a new cell if there isn't one available to recycle
// cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell = [AHCell blankCell];
}
// set the contents of the cell (whether it's a new one OR a recycled one)
NSString *vaultsPath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/Vaults"];
NSString *dictionaryPath = [NSString stringWithFormat:#"%#/%#",
vaultsPath,
[self.allVaults objectAtIndex:indexPath.row]];
NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:dictionaryPath];
cell.backgroundView = [AHCellCreation backgroundView];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.selectedBackgroundView = [AHCellCreation selectedBackgroundView];
// cell = [AHCellCreation createCellWithDictionary:dictionary Cell:cell];
[cell populateAHCellWithDictionary: dictionary];
return cell;
}
Update updated code to address second issue. Rework AHCell so that the class method, e.g. blankCell returns a new cell with the subviews set up and the instance method, e.g. populateAHCellWithDictionary: sets the content.

In that case AHCellCreation class must add the subviews to the cell and then set the text in one go? You need to layout the cell inside the if statement (add the subviews, UILabels, UIImageView etc, and set their frames etc). And set the content outside the if statement.
Basically whatever doesn't change in each row put inside the if statement, but what changes from row to row put outside the if statement.
This is because the code inside the if statement is only reached when the cell is created, almost always its the cells that are visible on the screen when the Table view loads are created.
When you scroll down the cells that disappear off the top of the screen are reused, and put at the bottom. This means that is you have 100 rows, it won't create 100 cells (it only creates the number of cells that can be visible on the screen at a time and the reuses those) , as this would consume a lot of memory, and the scrolling wouldn't be as smooth.

Related

iPhone sdk UITableView cell reuse identifier

I am using UITableView. When a book is downloaded i am adding a checkmark image to my table. And i have done this. But when am scrolling my table am getting the image even for non-downloaded books (i.e) my table cell are reused. For this i googled and checked my code with that. Everything seems to be the same. Here is my code,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIImage *cellImage= [cacheImage getCachedImage:[listOfThumImages objectAtIndex:indexPath.row]];
if(cellImage == nil)
{
DeviantDownload *download;
download = [DownloadsArray objectAtIndex:indexPath.row];
cellImage = download.image;
if (cellImage == nil)
{
download.delegate = self;
}
NSLog(#"cellImage%#",cellImage);
UIImageView *imgView;
static NSString *CellIdentifier = #"";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
imgView1 = [[UIImageView alloc] initWithFrame:CGRectMake(680, 60, 40, 40)];//self.view.bounds.size.width-
[imgView1 setImage:[UIImage imageNamed:#"Downloaded.png"]];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if([defaults objectForKey:#"firstRun"])
{
if([[[appDelegate selectDB] valueForKey:#"bookname"] containsObject:[listOfBooks objectAtIndex:indexPath.row]] )
{
NSString *cellValue = [listOfBooks objectAtIndex:indexPath.row];
cell.contentView addSubview:imgView1];
}
return cell;
}
}
}
What's wrong with my code? Kindly help me out. Thanking You.
If you want that a cell does not display the image then simply remove the imageview, for that cell. set a view.tag for the image view, and retrieve it, then remove it.
What you are doing now, is to add a image sub view as soon as you need it for a cell, and later reuse that cell for all other rows. But you never removed the imageview for cells that dont need it.
CheckMarks with UITableView is little complicated then what it seems. First of all you'll have to save the status of your row which already has the bookmark. And, compare it in your cellForRowAtIndexPath method. What you can do is create on NSMutableArray. Save 1 for downloaded book and 0 for non-downloaded book. In your cellForRowAtIndexPath method compare [array objectAtIndex:indexpath.row] and set the image accordingly.
Also, change :
static NSString *CellIdentifier = #"";
To :
static NSString *CellIdentifier = #"MyCell";

"Load more.." on tableView with custom cells - cell reusing is causing me some issues

I'm trying to implement "Load more..." on a tableView. I've done it, but I don't know if it's efficient.
The thing is that I have custom cells, and if I do like this:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"Cell";
ArticlesCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ArticlesCell" owner:self options:NULL];
cell = (ArticlesCell *) [nib objectAtIndex:0];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
tableView.backgroundColor = cell.backgroundColor;
if (indexPath.row <= (self.bookmarks.count - 1)) {
[self configureCell:cell atIndexPath:indexPath];
}else{
cell.textLabel.text = #"Load more...";
}
return cell;
}
It works great but what happens is it's reusing the cells, and if I scroll, every fifth cell (this is height 77.0) will have the label "Load more...", but actually do it's job as normal.
I found this workaround, but I don't know is it good and efficient.
Here it is:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"Cell";
if (indexPath.row <= (self.bookmarks.count - 1)) {
ArticlesCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ArticlesCell" owner:self options:NULL];
cell = (ArticlesCell *) [nib objectAtIndex:0];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
tableView.backgroundColor = cell.backgroundColor;
[self configureCell:cell atIndexPath:indexPath];
return cell;
}else{
UITableViewCell *cell = [[UITableViewCell alloc] init];
cell.textLabel.text = #"Load more...";
return cell;
}
}
As you can see I'm making the "Load more..." cell a simple UITableViewCell, and not reusing it, since it's only one. Is this good approach? Can you advice me in something better?
Thank you!
Another approach would be to use 2 different cell identifiers, one to identify and reuse (once initially created) an ArticlesCell and another to identify and reuse (once initially created) a "Load more..." UITableViewCell. At least then you will only create the "Load more..." UITableViewCell once rather than every time it scrolls into view.
static NSString *ArticleCellIdentifier = #"ArticleCell";
static NSString *LoadMoreCellIdentifier = #"LoadMoreCell";
The LazyTableImages Apple iOS sample project uses a similar approach (see the Classes/ RootViewController.m).
When you are click on loadmore button then increase the number of rows and reload the tableview . i.e in the method numberofrowsinsection.Let me know if you need any more

Horizontal scrollview within UITableview issue

I created UIScrollview with in UITableViewCell. The scrollView has some pictures to display in horizontally. It works good in one row but I am loading more than one row cells, it makes an issue when one cell is replaced by other cells.
I am using [tableView dequeueReusableCellWithIdentifier:section].
I also gave separate identifier for each row.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *section = [NSString stringWithFormat:#"section%#cell", indexPath.section];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:section];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:section];
NSArray *pageImages = imgs;
}
else
{
NSLog(#"recall");
}
return cell;
}
You don't want to give each cell a different reuse identifier. Change
NSString *section = [NSString stringWithFormat:#"section%#cell", indexPath.section];
to
NSString *section = #"Arbitrary";

iphone - UITableView scroll problem

I have created a TableView in my application with 5 sections in it.
Sections 1 - 4 only contain one row each for the minute and section 5 contains 5 rows.
Everything works fine until I scroll the TableView off the screen. In my first section and row (cell) I have the accessoryView set to a UILabel with some text in it.
Every other cell has the disclosure button as the accessoryType.
When I scroll the tableView the text I have in the first cell somehow appears in the the last cell!?
I have set up my data by adding NSStrings to array's and then adding them as dictionaries to an NSMutableArray.
And here is my cell set up:
- (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];
}
// UISwitch *aUISwitch = [[[UISwitch alloc]initWithFrame:CGRectZero]autorelease];
// Configure the cell.
NSDictionary *dictionary = [listOfSettings objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:#"Settings"];
NSString *cellValue = [array objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
if([cellValue isEqualToString:#"Status"]){
UILabel *viewLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 20)];
[viewLabel setText:#"Connected"];
cell.accessoryView = viewLabel;
[viewLabel release];
}
else{
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
}
return cell;
}
I know cells get deleted/removed when they go off screen so I presume this has something to do with it? What is the recommended practice for dealing with cells that go off screen and reappear?
just at quick glance... in the else statement, not only do you need to set the cell.accessoryType, but also set the cell.accessoryView=nil;
the accesoryView is still there as the cell was recycled.

problem in display in a tableView

as you see in the screen prints, tableView always displays a single value in the tableView "Title" My problem is to display all the array values ​​detailTableView
- (void)viewDidLoad {
NSArray* tmpArray = [[NSArray alloc] initWithObjects:#"Titre", #"Nom", #"Prenon",#"Adresse",#"Phone",#"Mobile",#"Mail",#"Site",#"Note",nil];
self.detailsTableView = tmpArray;
[tmpArray release];
[super viewDidLoad];
}
- (UITableViewCell *)tableView:(UITableView *)atableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
cell.textLabel.text =[self.detailsTableView objectAtIndex:indexPath.row] ;
return cell;
}
From the screen grab, it looks like you've only got one cell per section. In which case you need to use indexPath.section instead of indexPath.row.
From the apple Documentation
textLabel
Returns the label used for the main textual content of the table cell. (read-only)
#property(nonatomic, readonly, retain) UILabel *textLabel
Discussion
Holds the main label of the cell. UITableViewCell adds an appropriate label when you create the cell in a given cell style. See “Cell Styles” for descriptions of the main label in currently defined cell styles.
So Create a new UILabel and add it to textLabel
did you set as follows:
- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section {
return [self.detailsTableView count];
}