伙计们,我有tableview,它是从我的nsarray加载的,但在管理单元后,它在不正确的地方显示元素。生成细胞:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *currentArray = [comments objectAtIndex:indexPath.section];
NSDictionary *currentComment = (NSDictionary *)[currentArray objectAtIndex:indexPath.row];
static NSString *CellIdentifier = @"TitleCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
int commentLevel = [[currentComment objectForKey:@"level"] intValue];
NSString *commentText = [currentComment objectForKey:@"text"];
cell.textLabel.tag = 0xff ;
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont fontWithName:@"Verdana" size:17.0];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.text = commentText;
cell.textLabel.backgroundColor = [UIColor clearColor];
if (commentLevel > 0 && cell.imageView.image == nil) {
cell.imageView.image = [UIImage imageNamed:@"06-arrow-northwest.png"];
}
if (commentLevel == 0 && [[openedSections objectAtIndex:indexPath.section] boolValue] == NO) {
if ([currentArray count] > 1) {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
}
return cell;
}
编辑方式:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if ([[openedSections objectAtIndex:indexPath.section] boolValue] == NO) {
[openedSections replaceObjectAtIndex:indexPath.section withObject:[NSNumber numberWithInt:1]];
NSMutableArray *rows = [[NSMutableArray alloc] init];
int i = 1;
while (i < [[comments objectAtIndex:indexPath.section] count]) {
NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:indexPath.section];
[rows addObject:[path copy]];
[path release];
i = i + 1;
}
[tableView beginUpdates];
[tableView insertRowsAtIndexPaths:rows withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView endUpdates];
[rows release];
} else if (indexPath.row == 0) {
[openedSections replaceObjectAtIndex:indexPath.section withObject:[NSNumber numberWithInt:0]];
NSMutableArray *rows = [[NSMutableArray alloc] init];
int i = 1;
while (i < [[comments objectAtIndex:indexPath.section] count]) {
NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:indexPath.section];
[rows addObject:[path copy]];
[path release];
i = i + 1;
}
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:rows withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView endUpdates];
[rows release];
}
}
你有什么想法吗?
每当修改表中的单元格时,都需要更新NSArray,否则数据源和视图将不同步。
所以每当你打电话:
[tableView insertRowsAtIndexPaths:rows withRowAnimation:UITableViewRowAnimationAutomatic];
你应该放
for (NSIndexPath *indexPath in rows)
{
[self.myDataArray insertObject:myRowModel atIndex:indexPath.row];
}
同时,使您的视图和模型保持同步。
显然,上面的代码是伪代码(您必须创建一个模型对象的实例),并且您的数组必须是一个NSMutableArray,否则当您尝试插入它时它会崩溃。