2011-01-14 51 views
0

我有一个表视图。在每个单元格(行)中,我想显示两个按钮。最初都是红色的。当一个按钮被点击时,它会变成绿色,其他的会变成红色。 我创建了一个有两个按钮的视图。我正在使用IB创建视图。 我使用下面的代码来显示我的表视图来显示自定义视图:iPhone - UITableViewCell有UIView它有2个UIButton

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

NSArray *bundle = [[NSBundle mainBundle] loadNibNamed:@"CellView" 
      owner:self options:nil]; 
CellView *myView; 
    for (id object in bundle) { 
     if ([object isKindOfClass:[CellView class]]) 
      myView = (CellView *)object; 
    } 
NSString* str = [testArray objectAtIndex:indexPath.row]; 
NSArray* arr = [str componentsSeparatedByString:@" "]; 
myView.left.titleLabel.text = [arr objectAtIndex:0]; 
myView.right.titleLabel.text = [arr objectAtIndex:1]; 

[cell.contentView addSubview:myView]; 
    return cell; 
} 

上面的代码工作正常,但点击该按钮时,其显示的文字,我在IB创建的按钮。我不明白为什么会发生?

有人可以指导我如何在单元格中显示按钮并处理其操作?

回答

1

我认为你必须为不同的按钮状态指定titleLabel。然后这段代码将正常工作。

+0

没错。它是我的错误,以确定简单的事情。 – Satyam 2011-01-14 06:38:09