UITableViewCell没有得到更新

时间:2009-04-13 11:19:41

标签: iphone objective-c cocoa cocoa-touch

对于表格视图单元格中的每个条目,我需要一个右边的按钮然后是一些文本,然后是左边的一个按钮。在按钮单击事件中,我需要更改文本(单击左/右按钮。)并根据文本条件删除任一按钮。我无法使用cellForRowAtIndexPath方法删除按钮。我尝试了子类化UITableViewCell并使用方法-prepareForReuse,但我无法重置单元格。任何想法我如何实现这一目标?或者是否有某种方法可以使这个按钮隐藏或隐藏?

    NSString *CellIdentifier = [[NSString alloc] initWithFormat:@"Cell%d",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}


[self.tableView clearsContextBeforeDrawing];
NSString *str = [listOfItems objectAtIndex:indexPath.row];

    cell.text = [listOfItems objectAtIndex:indexPath.row];
    cell.textColor = [UIColor blueColor];
    cell.font = [UIFont systemFontOfSize:14];

        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];       
        UIImage *image = [UIImage imageNamed:@"Arrow_Right.png"];
        CGRect frame = CGRectMake(290, 5, image.size.width, image.size.height);
        button.frame = frame;   
        [button setBackgroundImage:image forState:UIControlStateNormal];
        button.backgroundColor = [UIColor clearColor];
        [button addTarget:self action:@selector(rightArrow_clicked:) forControlEvents:UIControlEventTouchUpInside];
        [cell addSubview:button];

        UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];      
        UIImage *img = [UIImage imageNamed:@"Arrow_Left.png"];
        CGRect frame1 = CGRectMake(5, 5, img.size.width, img.size.height);
        button1.frame = frame1;
        [button1 setBackgroundImage:img forState:UIControlStateNormal];
        button1.backgroundColor = [UIColor clearColor];
        [button1 addTarget:self action:@selector(leftArrow_clicked:) forControlEvents:UIControlEventTouchUpInside];
        [cell addSubview:button1];

    if([str isEqual:@" abc  "])
        [button setEnabled:NO];
    if([str isEqual:@" pqr "])
        [button1 setEnabled:NO];

1 个答案:

答案 0 :(得分:1)

您要做的是UITableViewCell的子类,并在init方法中添加子视图,以便它们只添加一次。

最有可能发生的事情是您将子视图添加到可重复使用的单元格中,该单元格很可能已经在之前的使用中具有按钮。此外,您可以将按钮设置为“隐形”,如下所示:

[button setHidden:YES];