我的TableView有三个不同的部分。第0部分用于上传照片,直到用户上传照片为止。但是,我的tableview正在重用第2节第1节中的单元格,我不知道为什么。这是下面的代码。非常感谢任何帮助。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"cell";
if (indexPath.section == 0) {
UploadsCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UploadsCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.tag = indexPath.row;
cell.delegate = self;
cell.imageInfo = [_uploads objectAtIndex:indexPath.row];
[cell setCellInfo];
cell.backgroundColor = [UIColor redColor];
return cell;
}
else{
id object;
if (indexPath.section == 1) {
object = [self cacheObjectAtIndexPath:indexPath];
return [self tableView:tableView cellForRowAtIndexPath:indexPath withObject:object];
}
if (indexPath.section == 2) {
object = [self objectAtIndexPath:indexPath];
}
return [self tableView:tableView cellForRowAtIndexPath:indexPath withObject:object];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath withMobiObject:(MobiObject *)object{
static NSString *CellIdentifier = @"Cell";
StylesCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = (StylesCell*)[[StylesCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (object) {
[cell setObject:object];
}
return cell;
}
答案 0 :(得分:1)
首先应该让您的方法更具可读性。既然你已经问我这个问题,我就根据他们的部分为单元格返回正确的配置时,重写了一个较大的方法来使用开关。
至于你的实际问题,它很可能与返回第1部分的配置的以下行相关联:
id object = [self cacheObjectAtIndexPath:indexPath];
return [self tableView:tableView cellForRowAtIndexPath:indexPath withObject:object];
以及第2节的其他行返回配置:
id object = [self objectAtIndexPath:indexPath];
return [self tableView:tableView cellForRowAtIndexPath:indexPath withObject:object];
我不确定这些方法究竟返回了什么,所以很难说它们到底是什么回复了重复的细胞。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.section) {
case 0:
// Section is Zero
UploadsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
cell.tag = indexPath.row;
cell.delegate = self;
cell.imageInfo = [_uploads objectAtIndex:indexPath.row];
[cell setCellInfo];
[cell setBackgroundColor:[UIColor redColor]];
return cell;
case 1:
// Section is One
id object = [self cacheObjectAtIndexPath:indexPath];
return [self tableView:tableView cellForRowAtIndexPath:indexPath withObject:object];
case 2:
// Section is Two
id object = [self objectAtIndexPath:indexPath];
return [self tableView:tableView cellForRowAtIndexPath:indexPath withObject:object];
default:
// Section is neither of the aforementioned sections.
break;
}
}