-1
伙计们,我有从我的nsarray加载的tableview,但是在管理单元格之后,它显示了错误位置的元素。 生成细胞:在uitableview中删除和添加行
- (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“comments”包含一些NSArrays,它包含所有单元格的NSDictionaries。但nsarray“openedSections”控制它。 – werbary 2012-02-03 06:05:10