如何在表视图中向单元格添加按钮

时间:2014-09-26 06:42:22

标签: ios

我需要在表格视图的单元格中添加两个可点击按钮。 每个单元格还有一个视图,其中有标签和图像。基本上我想要两个按钮,以便在点击一个按钮时加载表1并且可以加载其他按钮表2的点击。 提前谢谢。

1 个答案:

答案 0 :(得分:1)

以下示例代码可以帮助您

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString   stringWithFormat:@"Cell %i",indexPath.section]];

    if (cell == nil) {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:[NSString stringWithFormat:@"Cell %i",indexPath.section]] autorelease];

    }


    NSLog(@"i am here in table function..........now");


    //cell.textLabel.text = [data objectAtIndex: indexPath.row];

    search_items *pro = [searchCount objectAtIndex:[indexPath row]];

    cell.textLabel.text = pro.s_showroomName;



    UIButton* aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

aButton.frame = CGRectMake(0, 0,150,44);  

        [aButton setTag:[indexPath row]];

        [aButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];


    [cell.contentView addSubview:aButton];

        return cell;
}

-(void)buttonClicked:(UIButton*)sender {
    int tag = sender.tag;

///and rest code here.....

}