gpt4 book ai didi

ios - 在表格 View 中根据网络响应设置不同的按钮标题

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

我是 objective-c 的初学者。搜索了很多但没有解决我的问题。我在 tableview 中有一个单元格原型(prototype),它有多个标签和一个按钮。按钮将根据服务器端数据处于非事件状态和事件状态。例如,如果 feedback = close,按钮将处于非事件状态,仅显示文本关闭,如果 feedback = action,按钮将显示 Storyboard中设置的图像在细胞原型(prototype)制作过程中。单击按钮时将执行某些操作。我的问题是,我的 tableview 第一次按预期显示数据,但在滚动按钮上图像更改为文本关闭。这是我的代码。为清楚起见添加屏幕截图。我必须对按钮单击和行单击执行两个不同的操作。
screenshot

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

TransactionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"customTransactionCell" forIndexPath:indexPath];
if (cell==nil)
{
cell = [[TransactionCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"customTransactionCell"];
}
// Configure the cell.

if([[[transactionList objectAtIndex:indexPath.row]valueForKey:@"action"]isEqualToString:@"Close"]){
[cell.feedbackButton setImage:nil forState:UIControlStateNormal];
[cell.feedbackButton setTitle:@"Close" forState:(UIControlStateNormal)];

}
else
if([[[transactionList objectAtIndex:indexPath.row]valueForKey:@"action"]isEqualToString:@"Open"])
{
[cell.feedbackButton setImage:nil forState:UIControlStateNormal];
[cell.feedbackButton setTitle:@"Open" forState:(UIControlStateNormal)];
}
else
{
cell.feedbackButton.tag = indexPath.row;
[cell.feedbackButton addTarget:self action:@selector(feedbackButtonClick:) forControlEvents:UIControlEventTouchUpInside];
}
return cell;
}

最佳答案

您的单元格正在被重复使用,这意味着重复使用后的单元格将保存您在上次使用时为其设置的所有设置。这意味着在 last else (action:@selector(feedbackButtonClick:)) 中,单元格不会更改上次使用单元格时继承的视觉设置。您不得重置单元格(最好通过覆盖 -[TransactionCell prepareForReuse])或为您的逻辑的每个案例设置视觉效果,而不能跳过以前的设计设置。好吧,没有针对您的具体情况的示例,因为我不知道您的默认 UI 设置,但它类似于

- (void)prepareForReuse {
[super prepareForReuse];
[self.feedbackButton setImage:<DEFAULT IMAGE> forState:UIControlStateNormal];
[cell.feedbackButton setTitle:<DEFAULT TITLE> forState:(UIControlStateNormal)];
[cell.feedbackButton removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents];
}

关于ios - 在表格 View 中根据网络响应设置不同的按钮标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35204243/

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