在表格视图中添加button

我想知道….在表格行中的每个单元格中添加button…..以编程方式在运行时,还想单击时识别button….

我写一点代码..在这里….

//---insert individual row into the table view--- - (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 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..... } 

此代码无法正常工作…. :)请给我解决scheme:)

在此先感谢…..请给我任何教程也..

像这样设置你的button框架..

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

chck的问题,你可能会得到一些帮助运行时添加button和调用方法。 iPhone:在View或TableView中水平添加滚动条的UIButton?