gpt4 book ai didi

objective-c - 为什么 UITableViewCell 没有正确重新加载?

转载 作者:行者123 更新时间:2023-11-28 17:36:18 25 4
gpt4 key购买 nike

我将一堆国家加载到表中并允许用户删除。这些行直接对应于国家数组,通过使用 objectAtIndex:indexPath.row 并将删除按钮的标签设置为相同的索引。

当我删除前几行时没问题,但之后错误的行被删除并且应用程序崩溃。

这是我创建行的方法:

- (UITableViewCell *)tableView:(UITableView *)tableView 
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
NSString *currentText;

// fill in country cell content
if ([tableView tag] == 400) {
cell = [tableView dequeueReusableCellWithIdentifier:@"SettingsCountryCell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"SettingsCountryCell"];
NSLog(@"is nil");
} // it´s never nil

currentText = [arrSelectedCountries objectAtIndex:indexPath.row];
int currentRow = indexPath.row;

UILabel *countryLabel = (UILabel *)[cell viewWithTag:401];
countryLabel.text = currentText;

// create button
UIButton *countryButton = (UIButton *)[cell viewWithTag:402];

[countryButton setTag:currentRow];
[countryButton setTitle:[NSString stringWithFormat:@"row%d", currentRow]
forState:UIControlStateNormal];
NSLog(@"%@ - tag %d - title %@ - button tag %d",
currentText,
currentRow,
countryButton.titleLabel.text,
countryButton.tag);

// on load: Bulgaria - tag 2 - title row2 - button tag 2
// after one deletion (and ever after):
// Bulgaria - tag 1 - title (null) - button tag 0

[countryButton addTarget:self
action:@selector(removeCountry:)
forControlEvents:UIControlEventTouchUpInside];

}

以下是我如何删除它们(即国家/地区):

- (void)removeCountry:(UIButton *)countryButton
{
// removed country
NSString *currentText = [arrSelectedCountries objectAtIndex:countryButton.tag];

NSLog(@"removed country %@ at tag %d", currentText, countryButton.tag);

// ok: removed country All CEE Countries at tag 0
// ok: removed country Bulgaria at tag 0
// not ok: removed country Czech Republic at tag 1

// add to picker selection and remove from selected
[arrCountries addObject:currentText];
[arrSelectedCountries removeObject:currentText];

// refresh picker and country table
[countryPicker reloadAllComponents];

[countryTable reloadData];

[countryTable reloadSections:[NSIndexSet indexSetWithIndex:0]
withRowAnimation:UITableViewRowAnimationFade];

// position table and elements below it
[self repositionForCountryChange];
}

首先表格显示:第 0 行、第 1 行、第 2 行等。

删除几行后:第 1 行、第 2 行、第 3 行等。

一旦发生这种情况,标签就不再与标记正确对应。当我在克罗地亚点击删除时,删除标记为第 1 行的按钮,NSLog 说:

在标签 1 处删除了国家捷克共和国

...克罗地亚行未被删除,尽管它的删除按钮现在显示为第 2 行。

为什么我要从 NSLog 获取:

title (null) - button tag 0 

...当我在表格中看到有一个标题并且 removeCountry 可以访问按钮标签时,尽管不正确?

最佳答案

您已经设置了带有标签的按钮,以允许您将其标识为单元格 (402) 中的 subview ,然后您更改标签以标识标签所在的行。当该单元格被重新使用,没有带有标签 402 的 View ,因此您的按钮仍然用其原始行的标签号标识。

您将标签用于两个目的。理想情况下,根本不要使用它们,因为它们很脆弱并且不会使您的代码可读。

  • 创建一个 UITableViewCell 子类并使用 outlets 识别其 subview
  • 使用我描述的方法识别按下按钮的行here这比标签灵活得多,如果您删除行,它会继续工作。

关于objective-c - 为什么 UITableViewCell 没有正确重新加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9715340/

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