2014-09-26 122 views
-2

我需要在我的表格視圖的單元格中添加兩個可點擊按鈕。每個單元格還有一個視圖,其中有標籤和圖像。基本上我想要兩個按鈕,以便點擊一個按鈕表1被加載,並且點擊另一個按鈕表2可以被加載。 Thankx提前。如何將按鈕添加到表格視圖中的單元格

+2

在'的cellForRowAtIndexPath:'方法中,創建兩個按鈕和添加這樣 '[cell.contentView addSubView:按鈕1];'' [細胞。 contentView addSubView:button2];' – sreekanthk 2014-09-26 06:53:17

+0

[以編程方式向表格視圖單元格添加按鈕。](http://stackoverflow.com/questions/7721364/add-buttons-programatically-to-table-view-cells) – Deepak 2014-09-26 06:59:03

+0

@ Saksha:在發佈問題之前先將它google。 SO中已經有很多問題了。 – Deepak 2014-09-26 07:00:56

回答

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..... 

} 
相關問題