为什么我的UITableView单元格上的这个按钮重绘自己?

时间:2011-06-15 18:22:21

标签: ipad uitableview uibutton

我的tableview单元格上有一个按钮 但滚动后按钮看起来像这样:

enter image description here

看起来有时一个按钮高于另一个按钮 我怎么解决这个问题?

这是cellForRowAtIndexPath

中按钮的代码
        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

Chapter *chapter = nil ;
UITableViewCell *cell = nil;
NSUInteger row = [indexPath row];
switch (indexPath.section) {
    case 0: 
        chapter = [[einfuerung objectAtIndex:row]retain];
        break;
    case 1: 
        chapter = [[vertiefung objectAtIndex:row]retain];
        break;
    case 2: 
        chapter = [[spezial objectAtIndex:row]retain];
        break;
}


if ([self filesFromFolderWithChapter:chapter] > 1) 
{

    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
    cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
    if (cell == nil) 
    { 
        cell = [[[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:SimpleTableIdentifier] autorelease];
    }
}   
else {

    static NSString *ButtonTableIdentifier = @"ButtonTableIdentifier";
    cell = [tableView dequeueReusableCellWithIdentifier: ButtonTableIdentifier];
    if (cell == nil) 
    { 
        cell = [[[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ButtonTableIdentifier] autorelease];
    }

}



    NSString *firstString;
    NSString *lastString;

    switch (indexPath.section) {
        case 0: 
            chapter = [[einfuerung objectAtIndex:row]retain];
            [dataInstance extractFromString: [chapter title]: &firstString: &lastString];
            cell.textLabel.text = firstString; 
            cell.detailTextLabel.text = lastString;
            cell.backgroundColor = [UIColor whiteColor];


            break;
        case 1: 
            chapter = [[vertiefung objectAtIndex:row]retain];
            [dataInstance extractFromString: [chapter title]: &firstString: &lastString];
            cell.textLabel.text = firstString; 
            cell.detailTextLabel.text = lastString;
            cell.backgroundColor = [UIColor whiteColor];

            break;
        case 2: 
            chapter = [[spezial objectAtIndex:row]retain];
            [dataInstance extractFromString: [chapter title]: &firstString: &lastString];
            cell.textLabel.text = firstString; 
            cell.detailTextLabel.text = lastString;
            cell.backgroundColor = [UIColor whiteColor];

            break;
    }



if ([self filesFromFolderWithChapter:chapter] > 1) 
{


        //VersionButton *button = [VersionButton buttonWithType:UIButtonTypeRoundedRect];
        VersionButton *button = [[VersionButton alloc]initWithFrame:CGRectMake(500, 15, 150, 30) andTitle:@"ältere Versionen"];
        button.chapter = chapter;
        [button addTarget:self action:@selector(versionTapped:) 
         forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:button];   
        [button release];

}
    [chapter release];

return cell;

}

2 个答案:

答案 0 :(得分:2)

首先,查看整个cellForRowAtIndexPath函数会很有帮助。

看起来您没有正确考虑细胞重用。如果要从头开始创建单元格,而不是重复使用单元格,则只应向单元格添加按钮。如果正在重用一个单元格,您应该有办法访问它已有的按钮并对其进行必要的更改。

答案 1 :(得分:-1)

你是否在其他地方调用setNeedsDisplay会导致它调用drawRect?