gpt4 book ai didi

objective-c - 单元格 accessorytype 不会在 [tableview reloadData] 上重新加载

转载 作者:行者123 更新时间:2023-11-29 13:45:04 26 4
gpt4 key购买 nike

我是 iPhone 编程的新手,所以我的问题可能是由于完全不了解非常基本的原理造成的。

我正在开发一个有两个 View 的 iPhone 应用程序。第一个 View 有两个按钮,当按下其中一个按钮时,会弹出一个带有表格 View 的模态视图。根据按下的按钮,此 TableView 的填充方式不同。如果按下按钮 button1,tableview 将填充 button1Data。用户可以选择单元格,如果这样做,单元格 accessorytype 将设置为 UITableViewCellAccessoryCheckmark。然后,我将选中的单元格的名称保存到 tableViewListChecked 中,以便稍后可以在数据更改时决定应该检查哪个单元格。

问题如下:关闭 modalview 后,我选择了 button2,在 button1Data 中选择的单元格在 button2Data 中仍然被选中。我很清楚函数 [theTableView reloadData] 正在工作,因为数据确实发生了变化,但是 accesorytupes 仍然相同,直到我将单元格滚动到屏幕之外。

附言如果我确实滚动了屏幕的单元格,则 accessorytype 设置正确。

- (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];
cell.accessoryType = UITableViewCellAccessoryNone;
}

else if ([tableViewListChecked containsObject:[NSString stringWithString:cell.textLabel.text]]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

else {
cell.accessoryType = UITableViewCellAccessoryNone;
}

cell.textLabel.text = [tableViewList objectAtIndex:indexPath.row];

return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

if (![tableViewListChecked containsObject:[NSString stringWithString:cell.textLabel.text]]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[tableViewListChecked addObject:[NSString stringWithString:cell.textLabel.text]];
}

else if ([tableViewListChecked containsObject:[NSString stringWithString:cell.textLabel.text]]) {
cell.accessoryType = UITableViewCellAccessoryNone;
[tableViewListChecked removeObject:[NSString stringWithString:cell.textLabel.text]];
}
}

谢谢你的帮助!

最佳答案

如果单元格不是 nil(如果它已从 TableView 中出队),那么您实际上并没有改变附件类型。去掉else,即改

else if ([tableViewListChecked containsObject:[NSString stringWithString:cell.textLabel.text]]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

if ([tableViewListChecked containsObject:[NSString stringWithString:cell.textLabel.text]]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

关于objective-c - 单元格 accessorytype 不会在 [tableview reloadData] 上重新加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7717784/

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