views:

41

answers:

2

hi, i have been trying to figure this out but i just can't.... for some reason, everytime my UITableView reaches a certain length due to the number of rows and sections, cells seem to randomly copy themselves into different cells at the end of the table without me wanting or programming it... Anyone else have this issue or knows how it's resolved? Any help is appreciated! Tanks. Edit: The "row" property is a counter which gets counted up to 13 and is then reset to 0 and counted up again and i always want the string "newUpdate" to be displayed in the corresponding row but at the same time i don't want the rest of the cells to be blank i want them to keep their old content until they're overwritten because the counter is starting at 0 again.

#import "ServerUpdateViewController.h"


@implementation ServerUpdateViewController

@synthesize newUpdate;
@synthesize row;




- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section     {
return 15;
}

- (NSString *)tableView: (UITableView *)table titleForHeaderInSection:(NSInteger) section {
return @"All Updates to Server";

}

// Customize the appearance of table view cells.
- (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];
}


    if (indexPath.row == self.row) {
        cell.textLabel.text = self.newUpdate;
    }


// Set up the cell...

return cell;
}




- (void)dealloc {
[super dealloc];
}

@end

A: 

There are a few things that can cause this and I can't be specific without seeing code, but it comes down to the fact that cells are reused. The entire content of the cell will have to be re-set/redrawn every time cellForRowAtIndexPath is called.

Rob Lourens
i added the code and some explanatory comments above
Christoph v
You're going about this backwards. Assume that `cellForRowAtIndexPath` will be called on any random cell and whenever the system feels like it. It's your job to provide the correct data to the cell whenever it's called. If it's called on a cell that isn't self.row, you still need to provide the correct data, otherwise the string that's already in that reused cell will still be there. Think about that a little and maybe read some sample code on `UITableViewController` s.
Rob Lourens
A: 

You're re-using cells. In your cellForRowAtIndexPath: method, you need to fully configure the cell every time it's called.

Dave DeLong
Could you give an example? :-)
Christoph v
@Christoph v: every time this method is called, you need to configure the cell as if it were the first time it's being displayed. If some cells are displaying a string, then you must set the string on every cell (either to a display value or an empty string if you don't want anything to show up). Assume that if you're getting a cell, it could be dirty. Clean it up *completely* before returning it.
Dave DeLong