如何将按钮添加到表格视图中的单元格
问题描述:
我需要在我的表格视图的单元格中添加两个可点击按钮。每个单元格还有一个视图,其中有标签和图像。基本上我想要两个按钮,以便点击一个按钮表1被加载,并且点击另一个按钮表2可以被加载。 Thankx提前。如何将按钮添加到表格视图中的单元格
答
下面是示例代码可能它帮助你
- (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.....
}
在'的cellForRowAtIndexPath:'方法中,创建两个按钮和添加这样 '[cell.contentView addSubView:按钮1];'' [细胞。 contentView addSubView:button2];' – sreekanthk 2014-09-26 06:53:17
[以编程方式向表格视图单元格添加按钮。](http://stackoverflow.com/questions/7721364/add-buttons-programatically-to-table-view-cells) – Deepak 2014-09-26 06:59:03
@ Saksha:在发布问题之前先将它google。 SO中已经有很多问题了。 – Deepak 2014-09-26 07:00:56