iOS - UITableView更改数据源

时间:2013-08-14 20:47:28

标签: iphone ios objective-c uitableview

我想知道是否有人知道如何解决这个问题:

我有一个UITableview,数据源可以继续增长。问题是当我向数据源添加元素时,表格单元格变得混乱。

每个表格单元格都有一个文本字段,用户可以在其中输入数据,但每当我向数据源添加数据时,注释都会复制到其他单元格。

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

static NSString *cellIdentifier = @"ImageTableCell";
ImageTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

Picture *aPicture = (Picture *) [self.imageData objectAtIndex:[indexPath row]];

if (cell == nil)
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ImageTableCell" owner:self options:NULL];
    cell = (ImageTableCell *)[nib objectAtIndex:0];
}

NSLog(@"comment %@ index %d", aPicture.comment, [indexPath row]);

cell.cellImage.image = aPicture.picture;
cell.commentField.delegate = self;
cell.commentField.text = aPicture.comment;
cell.index = [indexPath row];
cell.tag = [indexPath row];

return cell;
}

self.imagedata是一个NSMutabaleArray。

修改

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.imageData count];
}

-(void) reloadImages:(NSNotification *) aNotification {

self.imageData = ((AppDelegate *)[[UIApplication sharedApplication] delegate]).currentUserData.images;

[self.imageTableView reloadData];
}

a screenshot http://www.mikerizzello.com/pic.png

由于

2 个答案:

答案 0 :(得分:0)

编辑:

我认为你可能有一个糟糕的MVC模式。这就是我要做的事情:

获取自定义单元格ImageViewCell,并将该类设为textField委托。为自定义图片对象添加属性,并在cellForRow ...

中设置该属性

然后,在自定义单元格类中的textFieldDelegate方法中,可以更改设置PictureObject的内容。我有一种感觉,你在viewController中处理文本输入,并且数组中数据对象的索引混淆了,你将注释字段文本应用于数组中的错误项目。

修改

我认为它与细胞再利用有关。重复使用细胞时,细胞内容不会消失。

在返回单元格之前尝试此块:

if (!aPicture.comment) {
    cell.commentField.text = @"";
} else {
    cell.commentField.text = aPicture.comment;
}

答案 1 :(得分:0)

你试试

吗?
if (cell == nil) {
    cell = (ImageTableCell*)[[ImageTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}

无论如何你也可以为每个单元格设置不同的reuseIdentifier,但这不是一个好主意,我想帮助你,但我认为需要运行这个项目。

如果上面的代码提供更好的解决方案,请告诉我。