gpt4 book ai didi

ios - TableView 单元格重用导致按钮标题在滚动时发生变化

转载 作者:行者123 更新时间:2023-11-29 12:19:47 25 4
gpt4 key购买 nike

我有一个显示名称和标题为“关注”的按钮的列表。当我点击按钮时,标题应更改为“取消关注”。如果我再次点击按钮,标题应该再次变为“关注”。这工作正常,但当我滚动表格时,其他单元格的标题也由于单元格重用而发生变化。

代码如下:

  - (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"];


}


}

请提出一些防止电池重复使用的建议。

最佳答案

在 cellForRowAtIndexPath 中的 followOrUnfollow 中也添加此条件

if ([sender.titleLabel.text isEqualToString:@"Follow"]) {
[sender setTitle:@"UnFollow" forState:UIControlStateNormal];

}
else if ([sender.titleLabel.text isEqualToString:@"UnFollow"]) {
[sender setTitle:@"Follow" forState:UIControlStateNormal];


}

关于ios - TableView 单元格重用导致按钮标题在滚动时发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30972724/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com