Iphone UITableView在重新加载数据时如何创建UITableViewCell的问题
我知道也有类似的问题,但这些被认可的答案似乎对我不起作用。因此,我的场景是,我有一个Iphone UITableView在重新加载数据时如何创建UITableViewCell的问题,iphone,objective-c,uitableview,Iphone,Objective C,Uitableview,我知道也有类似的问题,但这些被认可的答案似乎对我不起作用。因此,我的场景是,我有一个UITableView,我想通过扫描条形码来添加和删除项目。所有这些都可以正常工作,只是我无法使用UITableView来显示更新的信息。问题具体来自于在初始重新加载之后每隔一次重新加载时的tableView:cellforrowatinexpath:方法。更具体地说,单元始终是非nil,因此它跳过了新单元创建逻辑 对于像我这样的其他问题,答案是单元格标识符就是问题所在。嗯,我试着胡闹,但没用。这是我的密码: -
UITableView
,我想通过扫描条形码来添加和删除项目。所有这些都可以正常工作,只是我无法使用UITableView
来显示更新的信息。问题具体来自于在初始重新加载之后每隔一次重新加载时的tableView:cellforrowatinexpath:
方法。更具体地说,单元始终是非nil
,因此它跳过了新单元创建逻辑
对于像我这样的其他问题,答案是单元格标识符就是问题所在。嗯,我试着胡闹,但没用。这是我的密码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
if (indexPath.section < vehicle.inventoryCategoriesCount) {
cell = [tableView dequeueReusableCellWithIdentifier:@"ModelCell"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"ModelCell"] autorelease];
NSString *model = [[[vehicle.inventory filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"category == %@", [vehicle.inventoryCategories objectAtIndex:indexPath.section]]] valueForKeyPath:@"@distinctUnionOfObjects.model"] objectAtIndex:indexPath.row];
cell.textLabel.text = model;
cell.detailTextLabel.text = [NSString stringWithFormat:@"%d", [[vehicle.inventory filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"model == %@", model]] count]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
} else {
cell = [tableView dequeueReusableCellWithIdentifier:@"RemoveCell"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"RemoveCell"] autorelease];
cell.textLabel.text = @"Remove an Item";
cell.textLabel.textColor = [UIColor redColor];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
}
return cell;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
UITableViewCell*单元格=nil;
if(indexPath.section
因此,在这个示例代码中,我有两个不同的单元标识符用于单独的部分。它们是ModelCell和RemoveCell。嗯,它们不能作为解决方案,因为什么都没有发生。如果我在分配新单元时更改单元标识符,它会工作,因为它只是在擦除所有内容,因为标识符不匹配,但我会假设这是错误的,应该有更好的解决方案来解决这个问题,或者我只是做得不对
我希望能在这方面得到一些帮助。到目前为止,我已经花了一天的时间在这段代码上,但我还没有找到任何地方,我想把它修好,然后转到我应用程序的其他部分
提前感谢您的帮助
更新
多亏了@fluchtpunkt,问题才得以解决。对于将来可能遇到这种情况的任何其他人,下面是更正的代码。我决定通过将节号附加到标识符上,使其更加唯一
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
UITableViewCell*单元格=nil;
if(indexPath.section
更新
最终的代码版本纠正了我对标识符如何工作的误解。我想我应该保持简单,所以我以单元格样式类型命名标识符
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
if (indexPath.section < vehicle.inventoryCategoriesCount) {
cell = [tableView dequeueReusableCellWithIdentifier:@"Value1"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Value1"] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
NSString *model = [[[vehicle.inventory filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"category == %@", [vehicle.inventoryCategories objectAtIndex:indexPath.section]]] valueForKeyPath:@"@distinctUnionOfObjects.model"] objectAtIndex:indexPath.row];
cell.textLabel.text = model;
cell.detailTextLabel.text = [NSString stringWithFormat:@"%d", [[vehicle.inventory filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"model == %@", model]] count]];
} else {
cell = [tableView dequeueReusableCellWithIdentifier:@"Default"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Default"] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.textColor = [UIColor redColor];
}
cell.textLabel.text = @"Remove an Item";
}
return cell;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
UITableViewCell*单元格=nil;
if(indexPath.section- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
if (indexPath.section < vehicle.inventoryCategoriesCount) {
cell = [tableView dequeueReusableCellWithIdentifier:@"Value1"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Value1"] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
NSString *model = [[[vehicle.inventory filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"category == %@", [vehicle.inventoryCategories objectAtIndex:indexPath.section]]] valueForKeyPath:@"@distinctUnionOfObjects.model"] objectAtIndex:indexPath.row];
cell.textLabel.text = model;
cell.detailTextLabel.text = [NSString stringWithFormat:@"%d", [[vehicle.inventory filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"model == %@", model]] count]];
} else {
cell = [tableView dequeueReusableCellWithIdentifier:@"Default"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Default"] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.textColor = [UIColor redColor];
}
cell.textLabel.text = @"Remove an Item";
}
return cell;
}
if (indexPath.section < vehicle.inventoryCategoriesCount) {
cell = [tableView dequeueReusableCellWithIdentifier:@"ModelCell"];
if (cell == nil) {
// only create a new cell if a dequeue was not successful.
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"ModelCell"] autorelease];
}
// whatever happened before you have a valid cell here.
// configure cell:
NSString *model = [[[vehicle.inventory filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"category == %@", [vehicle.inventoryCategories objectAtIndex:indexPath.section]]] valueForKeyPath:@"@distinctUnionOfObjects.model"] objectAtIndex:indexPath.row];
cell.textLabel.text = model;
cell.detailTextLabel.text = [NSString stringWithFormat:@"%d", [[vehicle.inventory filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"model == %@", model]] count]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
cell = [tableView dequeueReusableCellWithIdentifier:@"ModelCell"];
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
cell.textLabel.text = model;
cell.detailTextLabel.text = [NSString stringWithFormat:@"%d", [[vehicle.inventory filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"model == %@", model]] count]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;