I am working on one application where i am fetching some data in uitableview and when i am scrolling that table,it duplicates value. I know that it happens because i am using reuse identifier to initialize the cell. But then what is the rem开发者_如何学JAVAedy to neglect this problem?I am using below code.What if i don't want to use reuse property?
CellForClient *cell = (CellForClient *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[[CellForClient alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
}
Any solution?Thanks in advance.
EDIT:
@Erik B & RedBlueThing: Thanks for your reply but this is what i am already doing and getting duplicated value as i am using reuseIdentifier to initialize the the cell. Let me give u example: I am having 6 rows in my table.Each cell contains 7 inner values like first name,last name and some other details.So at the beginning i am showing 3 cells when i load the table.When i scroll table down and up again,the last rows' data overwrites the upper values and with each scroll,the values gets overwritten.
Any solution for my this problem.
This is how you should do it:
- (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];
}
// Configure the cell...
cell.textLabel.text = [NSString stringWithFormat:@"Cell %d", indexPath.row];
return cell;
}
If you look at the // Configure the cell...
comment. That's where you should always set the data. If you don't, you might get a reused cell that used to be on another position and since you're not reseting it, it will still show the data for the old position.
You should take advantage of cell reuse with your UITableView. Just make sure you are setting the textLabel.text property on the cell to the new value.
So the line after the if statement should be:
cell.textLabel.text = data for this row ...
You can test this is working by setting the text to something recognizable:
cell.textLabel.text = [NSString stringWithFormat:@"row %d", indexPath.row];
So even if you are reusing cells, you will get "row 0", "row 1" etc ...
Edit for update
So you need to make sure that you are re-setting all the inner values after you get your cell object, either from the allocation or the dequeueReusableCellWithIdentifier.
I had a similar issue and this is what I did to fix it. I created a custom class of type UITableViewCell and assigned it as the Custom class for that table cell in story board. Then added a label in the cell and created a outlet for that label in the above custom class.
@interface SystemCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *systemLbl;
@end
In cellForRowAtIndexPath used the above custom class instead of UITableViewCell
static NSString *cellIdentifier = @"cell";
SystemCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell=[[SystemCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
//other code
UILabel *systemLbl = cell.systemLbl;
systemLbl.text = systemName;
return cell;
I use this in situations where I have to add a button for each row, this way I can identify the correct button. Otherwise it get duplicated when we scroll due to cell reuse.
Hope this help for anyone having a similar issue.
精彩评论