重叠细胞 - 内容

时间:2010-12-15 21:56:58

标签: iphone ios uitableview

最近我发现了一个非常烦人的错误:

两个单元格(表格中的第一个单元格和表格中的最后一个单元格)的内容在一个单元格中混合(在表格的第一个和最后一个单元格中)。只有在我的表中有10个条目并添加第11个(见图片)时才会出现这种情况。

您是否知道这种行为会如何发生?

此致 的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;}

1 个答案:

答案 0 :(得分: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;}
相关问题