2010-12-15 70 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;}