2010-12-15 63 views
0

最近我发现了一个非常恼人的错误:重叠细胞含量

两个单元(在表中的第一个单元格,并在表格的最后一个单元)在一个细胞混合起来的内容(在第一和表格的最后一个单元格)。这只会发生,如果我在我的表中10个条目,并添加第11(见图片)。

你知道这种行为会发生吗?

问候, 萨沙

http://i.stack.imgur.com/g9fkj.jpg

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

UITableViewCell *cell = nil; 

static NSString *kCellTextField_ID = @"CellTextField_ID"; 
cell = [tableView dequeueReusableCellWithIdentifier:kCellTextField_ID]; 
if (cell == nil) 
{ 
// a new cell needs to be created 
cell = [[[UITableViewCellGradient alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellTextField_ID] autorelease]; 

cell.backgroundColor = [UIColor clearColor]; 
cell.textLabel.backgroundColor = [UIColor clearColor]; 
cell.textLabel.textColor = [UIColor blackColor]; 

cell.selectionStyle = UITableViewCellSelectionStyleNone; 
} 
else 
{ 
// a cell is being recycled, remove the old edit field (if it contains one of our tagged edit fields) 
UIView *viewToCheck = nil; 
viewToCheck = [cell.contentView viewWithTag:1]; 
if (!viewToCheck) { 
    [viewToCheck removeFromSuperview]; 
} 
} 

NSUInteger row = [indexPath row]; 

if (indexPath.section == 0) { 

id objId = [[self.data objectAtIndex:row] valueForKey:kViewKey]; 

if ([objId isKindOfClass:[UITextField class]]) { 
    UITextField *textField = [[self.data objectAtIndex:row] valueForKey:kViewKey]; 
    [cell.contentView addSubview:textField]; 
} else if ([objId isKindOfClass:[UISegmentedControl class]]) { 
    UISegmentedControl *segmentedField = [[self.data objectAtIndex:row] valueForKey:kViewKey]; 
    [cell.contentView addSubview:segmentedField]; 
} 
} 

if (row == 0) { 
[cell setPosition:UACellBackgroundViewPositionTop]; 
} else if (row == ([data count] - 1)) { 
[cell setPosition:UACellBackgroundViewPositionBottom]; 
} else { 
[cell setPosition:UACellBackgroundViewPositionMiddle]; 
} 



return cell;} 
+0

貌似造成在你身边不正确的实施UITableViewSourceDelegate的未定义行为。 (细胞的缓存可能非常复杂) 你能告诉我们一些代码吗? – 2010-12-15 22:10:42

+0

如果您可以发布代码,请。我曾经做过类似的事情,结果发现我在cellForRowAtIndexPath方法中做错了。 – hol 2010-12-15 22:18:35

+0

您应该使用“答案”中的代码更新您的问题,并删除“答案”。 textField和segmentedField具有什么标记值?你确定他们在细胞回收时被移除? – Anna 2010-12-15 23:16:23

回答

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

UITableViewCell *cell = nil; 

static NSString *kCellTextField_ID = @"CellTextField_ID"; 
cell = [tableView dequeueReusableCellWithIdentifier:kCellTextField_ID]; 
if (cell == nil) 
{ 
    // a new cell needs to be created 
    cell = [[[UITableViewCellGradient alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellTextField_ID] autorelease]; 

    cell.backgroundColor = [UIColor clearColor]; 
    cell.textLabel.backgroundColor = [UIColor clearColor]; 
    cell.textLabel.textColor = [UIColor blackColor]; 

    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
} 
else 
{ 
    // a cell is being recycled, remove the old edit field (if it contains one of our tagged edit fields) 
    UIView *viewToCheck = nil; 
    viewToCheck = [cell.contentView viewWithTag:1]; 
    if (!viewToCheck) { 
     [viewToCheck removeFromSuperview]; 
    } 
} 

NSUInteger row = [indexPath row]; 

if (indexPath.section == 0) { 

    id objId = [[self.data objectAtIndex:row] valueForKey:kViewKey]; 

    if ([objId isKindOfClass:[UITextField class]]) { 
     UITextField *textField = [[self.data objectAtIndex:row] valueForKey:kViewKey]; 
     [cell.contentView addSubview:textField]; 
    } else if ([objId isKindOfClass:[UISegmentedControl class]]) { 
     UISegmentedControl *segmentedField = [[self.data objectAtIndex:row] valueForKey:kViewKey]; 
     [cell.contentView addSubview:segmentedField]; 
    } 
} 

if (row == 0) { 
    [cell setPosition:UACellBackgroundViewPositionTop]; 
} else if (row == ([data count] - 1)) { 
    [cell setPosition:UACellBackgroundViewPositionBottom]; 
} else { 
    [cell setPosition:UACellBackgroundViewPositionMiddle]; 
} 



return cell;}