gpt4 book ai didi

iphone - 我的 UITable 正在重复使用不应该使用的单元格!

转载 作者:行者123 更新时间:2023-12-03 20:56:55 25 4
gpt4 key购买 nike

我有一个 UITableViewController,它有两个部分。第一部分显示一个带有居中文本的单元格,内容为添加新幻灯片。第二部分显示当前幻灯片。

当用户点击添加新幻灯片单元格时,新的UITableVeiwController会被推送到显示编辑器的堆栈上。如果用户保存新幻灯片(通过点击保存),单元格将添加到数据源,并且编辑器将从堆栈中弹出。

我有两个问题:

  1. 弹出编辑器时,如果在点击添加新幻灯片之前删除了单元格,则会显示旧单元格而不是新单元格。弹出 UITableViewController(通过点击自动生成的后退按钮)可以解决此问题,但我希望这种情况根本不会发生。 (本来弹出编辑器后弹出表格并没有更新,所以我在viewDidAppear方法中添加了[self.tableView reloadData];。)

    <
  2. 经过一定数量的幻灯片后,列表中的最后一张幻灯片将成为添加新幻灯片单元格。我知道数据输入正确,因为使用相同数据源的应用程序的另一部分可以正确更新。该表格支持在第二部分进行编辑,当您交换单元格的顺序时,它在幕后的行为正确,但错误的单元格仍然存在。

可能发生了什么?

这是我的一些代码:

请注意,当我准备发布代码时,我注意到大括号不匹配。对cell==nil 的检查似乎包含了确定单元格内容的代码的第二部分。这修复了表格第二部分中单元格的标签,但样式仍然错误。我已经修复了代码,但原始代码发布在此处。

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
if ([indexPath section] == 0 ) {
cell = [[[MBTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}else if([indexPath section] == 1){
cell = [[[MBTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

}

if ([indexPath section] == 0) {
[cell.textLabel setTextAlignment:UITextAlignmentCenter];
[cell.textLabel setText:@"Add a New Slide"];
}else if([indexPath section] == 1){
NSArray *storedViewsInfo = [[NSArray alloc] initWithArray:[kSettings arrayForKey:@"views"]];

if ([[[storedViewsInfo objectAtIndex:[indexPath row]] valueForKey:@"type"] isEqualToString:@"announcement"]) {
[cell.detailTextLabel setText:@"Custom Announcement"];
[cell.textLabel setText:[[[storedViewsInfo objectAtIndex:[indexPath row]] valueForKey:@"info"] valueForKey:@"text"]];
}

[storedViewsInfo release];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];

}
}
return cell;

}

最佳答案

在没有看到代码的情况下,首先想到的是检查您是否在 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; 中为自定义单元格指定了不同的标识符方法?

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

static NSString *CellIdentifier1 = @"CellIdentifier1";
static NSString *Cellidentifier2 = @"CellIdentifier2";

if (indexPath.section == kAddSlideSection) {
CustomCell *cellType1 = (CustomCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
...
} else {
CustomCell *cellType2 = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
...
}
}

此外,可能值得考虑实现一个委托(delegate)方法,当用户完成添加新幻灯片时调用该方法 - 即如果成功调用 [self.tableview reloadData]从该方法而不是 viewWillAppear 中.

关于iphone - 我的 UITable 正在重复使用不应该使用的单元格!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4381768/

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