UITableView,每个单元格中有多个项目

时间:2011-03-21 15:42:03

标签: iphone objective-c ios uitableview uibutton

我有一个tableview,我试图在其中放置一个带有图像和标签的按钮。我想在点击后更改按钮的图像。

以下是代码:

- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";
checkedImg = [UIImage imageNamed:@"buttonUnChecked1.png"];

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


//Set up the cell...
NSString *cellValue = [suggestions objectAtIndex:indexPath.row];

cell.selectionStyle = UITableViewCellSelectionStyleNone;

check = [UIButton buttonWithType:UIButtonTypeCustom];
check.frame=CGRectMake(0,35,20,20);
[check setImage:checkedImg forState:UIControlStateNormal];
[check addTarget:self action:@selector(checkClicked:) forControlEvents: UIControlEventTouchUpInside];
[cell.contentView addSubview:check];

cellContent = [[UILabel alloc]initWithFrame:CGRectMake(40,32,500,25)];
cellContent.text = cellValue;
[cell.contentView addSubview:cellContent];
return cell;
}


-(void)checkClicked:(UIButton *)b
{
checkedImg = [UIImage imageNamed:@"buttonChecked1.png"];
[check setImage:checkedImg forState:UIControlStateNormal];

}

通过这样做,按钮的图像会发生变化,但只有最后一个而不是单击的图像。我知道背后的原因,但我不知道如何实现我的目标。

2 个答案:

答案 0 :(得分:3)

获得您正在寻找的结果的结构化方法:

为您的表格单元格(包含按钮和标签)创建一个UIView子类。您实例化这些自定义视图,并将其设置为contentView中每个表格单元格的cellForRowAtIndexPath

每个自定义视图都会侦听自己按下的按钮。当它被按下时,它切换它的状态并告诉主视图控制器(通过委托方法)它被切换。主视图控制器调用相关单元格上的reloadData,使其重新加载正确的外观。

请注意,此方法要求您告诉每个自定义视图在表中呈现哪个索引路径 - 这样它可以通知主视图控制器的委托方法 - 触发重新加载需要此信息适当的细胞。

顺便说一句,我认为你想要在用户完成编辑时查看表中按钮的状态,而你当前的方法并没有非常明确地捕获状态 - 你必须迭代您的按钮,或将所选项目添加到可变数组或其他内容。

答案 1 :(得分:2)

您问题的简单答案是将checkClicked:方法更改为:

-(void)checkClicked:(UIButton *)b
{
    [b setImage:[UIImage imageNamed:@"buttonChecked1.png"] forState:UIControlStateNormal];
}

但您还应该调整tableView:cellForRowAtIndexPath:方法,以避免重复创建按钮并清除一些内存问题,如下所示:

- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";    

UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    UIButton *checkBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    checkBtn.frame = CGRectMake(0,35,20,20);
    [checkBtn setImage:[UIImage imageNamed:@"buttonUnChecked1.png"]; forState:UIControlStateNormal];
    [checkBtn addTarget:self action:@selector(checkClicked:) forControlEvents: UIControlEventTouchUpInside];
    [cell.contentView addSubview:checkBtn];

    UILabel *cellContentLbl = [[UILabel alloc]initWithFrame:CGRectMake(40,32,500,25)];
    cellContentLbl.tag = 1;
    [cell.contentView addSubview:cellContentLbl];
    [cellContentLbl release];
}


//Set up the cell...
NSString *cellValue = [suggestions objectAtIndex:indexPath.row];
cellContent = (UILabel *)[cell viewWithTag:1];
cellContent.text = cellValue;
return cell;
}
相关问题