【发布时间】:2015-06-22 05:39:39
【问题描述】:
我有一个显示名称和标题为“关注”按钮的列表。当我点击按钮标题应更改为“取消关注”。如果我再次点击按钮,标题应再次更改为“关注”。这工作正常,但是当我滚动表格时,其他单元格的标题也会由于单元格重用而发生变化。
代码如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"AuthorsListCell";
AuthorsListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier
forIndexPath:indexPath];
dic_eachAuthor = [[_arr_authors objectAtIndex:indexPath.row] mutableCopy];
cell.lbl_authorName.text = [dic_eachAuthor objectForKey:@"name"];
cell.btn_followOrUnfollow.tag = indexPath.row;
if([dic_eachAuthor valueForKey:@"follow"]){
[cell.btn_followOrUnfollow setTitle:@"UnFollow" forState:UIControlStateNormal];
}
else{
[cell.btn_followOrUnfollow setTitle:@"Follow" forState:UIControlStateNormal];
}
// action button method declarations
[cell.btn_followOrUnfollow addTarget:self action:@selector(followOrUnfollow:) forControlEvents:UIControlEventTouchUpInside];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
-(void)followOrUnfollow:(UIButton *)sender
{
if ([sender.titleLabel.text isEqualToString:@"Follow"]) {
[sender setTitle:@"UnFollow" forState:UIControlStateNormal];
[dic_eachAuthor setValue:@"1" forKey:@"follow"];
}
else if ([sender.titleLabel.text isEqualToString:@"UnFollow"]) {
[sender setTitle:@"Follow" forState:UIControlStateNormal];
[dic_eachAuthor setValue:nil forKey:@"follow"];
}
}
请提出一些防止单元重复使用的建议。
【问题讨论】:
-
为“如果单元格为 nil”添加代码,因为每次初始化新单元格时。所以你正面临这个问题。 cell = (ListTableViewCell *)[[ListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];
-
不,它不能解决问题。
标签: ios uitableview uibutton reuseidentifier