2010-12-15 15 views
0

vor kurzem entdecken ich einen wirklich ärgerlichen Fehler:Überlappende Zellen-Inhalt

den Inhalt von zwei Zellen (die erste Zelle in der Tabelle und die letzte Zelle in der Tabelle) in einer Zelle (in dem ersten vermischen und die letzte Zelle der Tabelle). Dies tritt nur auf, wenn ich 10 Einträge in meiner Tabelle habe und einen 11. hinzufüge (siehe Bild).

Haben Sie eine Idee, wie ein solches Verhalten auftreten kann?

Grüße, Sascha

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

Sieht aus wie ein undefiniertes Verhalten durch eine falsche Umsetzung der UITableViewSourceDelegate auf Ihrer Seite verursacht. (Das Zwischenspeichern von Zellen kann ziemlich kompliziert sein) Können Sie uns etwas Code zeigen? –

+0

Wenn Sie den Code bitte posten könnten. Ich habe etwas ähnliches einmal und es stellte sich heraus, dass ich es falsch in der CellForRowAtIndexPath-Methode gemacht habe. – hol

+0

Sie sollten Ihre Frage mit dem Code in Ihrer "Antwort" aktualisieren und die "Antwort" löschen. Welchen Tag-Wert haben das textField und das segmentierteField? Sind Sie sicher, dass sie entfernt werden, wenn die Zelle recycelt wird? – Anna

Antwort

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;}