向IOS7 Tableview添加多个按钮

向IOS7 Tableview添加多个按钮,ios,uitableview,uibutton,Ios,Uitableview,Uibutton,如何在每个单元格的tableview中添加几个按钮?我需要在我的tableview中的每个单元格中添加一个comment和like按钮,每个按钮都必须特定于被单击的行。我该怎么做这样的事情呢?我是不是在你的电脑里放了一个动作 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { [cell.button1 addTarget:self ac

如何在每个单元格的tableview中添加几个按钮?我需要在我的tableview中的每个单元格中添加一个comment和like按钮,每个按钮都必须特定于被单击的行。我该怎么做这样的事情呢?我是不是在你的电脑里放了一个动作

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
[cell.button1 addTarget:self action:@selector(button1Pressed:) forControlEvents:UIControlEventTouchUpInside];                                 In the 
-(void)button1Pressed:(id)sender{
    UIButton *button1 = (UIButton*)sender; 
    int selectedRow = sender.tag;
}

或者有没有一种方法可以通过编程从cellForRowAtIndexPath内部为iAction设置一个按钮,允许我发送参数?

您需要对UITableViewCell进行子类化,以便根据需要对其进行自定义

[cell.button1 addTarget:self action:@selector(button1Pressed:) forControlEvents:UIControlEventTouchUpInside];                                 In the 
-(void)button1Pressed:(id)sender{
    UIButton *button1 = (UIButton*)sender; 
    int selectedRow = sender.tag;
}

有很多关于这方面的教程。取决于您是否正在使用故事板。以下是其中之一:

您可以创建自定义表格视图单元格并添加多个按钮。
[cell.button1 addTarget:self action:@selector(button1Pressed:) forControlEvents:UIControlEventTouchUpInside];                                 In the 
-(void)button1Pressed:(id)sender{
    UIButton *button1 = (UIButton*)sender; 
    int selectedRow = sender.tag;
}
1.创建使用xib对UITableviewcell进行子类化的类 2.在xib中,删除视图并将tableview单元格拖动到xib中 3.在xib中添加多个按钮 4.为每个按钮创建IBOutlet。 5.在视图控制器中,导入CustomTableViewCell和CellForRowatineXpath方法,为每个方法添加操作,并将按钮标记设置为indexpath.row 6.从标记中识别单击按钮的indexpath

[cell.button1 addTarget:self action:@selector(button1Pressed:) forControlEvents:UIControlEventTouchUpInside];                                 In the 
-(void)button1Pressed:(id)sender{
    UIButton *button1 = (UIButton*)sender; 
    int selectedRow = sender.tag;
}

[cell.button1 addTarget:self action:@selector(button1Pressed:) forControlEvents:UIControlEventTouchUpInside];                                 In the 
-(void)button1Pressed:(id)sender{
    UIButton *button1 = (UIButton*)sender; 
    int selectedRow = sender.tag;
}

我想这就是你要问的

[cell.button1 addTarget:self action:@selector(button1Pressed:) forControlEvents:UIControlEventTouchUpInside];                                 In the 
-(void)button1Pressed:(id)sender{
    UIButton *button1 = (UIButton*)sender; 
    int selectedRow = sender.tag;
}
接收方方法您可以获取发送方的标签

[cell.button1 addTarget:self action:@selector(button1Pressed:) forControlEvents:UIControlEventTouchUpInside];                                 In the 
-(void)button1Pressed:(id)sender{
    UIButton *button1 = (UIButton*)sender; 
    int selectedRow = sender.tag;
}

在iTunes U或web上有大量教程,介绍如何创建自定义的
UITableViewCell
并使用
Objective C
Cocoa Touch
编写程序。Ray Wenderlich的网站是一个很好的起点(除了iTunes U上的斯坦福课程):这不是问题,我很清楚如何创建自定义表格单元格,问题是获取“objectAtIndex”传递到动作中,以便确定单击的状态。问题的第一部分是
如何在每个单元格的tableview中添加几个按钮?
,答案如下。第二部分我不完全理解,因为它不是很清楚,但我会让
UITableViewCell
子类在其内部处理
iAction
。如果您需要将数据发送回
UITableViewController
,您可以使用委托。这很接近,但当您在CellForRowAtIndexPath方法中说“为每个方法添加操作并将按钮标记设置为indexpath.row 6”时,我会失去您。ik如何将按钮链接到操作,但我不知道如何让该操作知道正在单击哪一行。一个片段会非常有用我想这就是你要问的。。。[cell.button1 addTarget:self action:@selector(button1按下:)for controlEvents:UIControlEventTouchUpInside];在receiver方法中,您可以按下sender的标记-(void)button1:(id)sender{UIButton button1=(UIButton)sender;int selectedRow=sender.tag;}我知道如何对单元格进行子类化并创建自定义单元格,但它并没有以任何明确的方式回答我的问题。我需要连接的操作来知道单击了哪一行,这样我就可以从用于创建tableview的api响应中获取正确的数据,这样我就可以采取适当的操作是的,这是正确的。在单元格中创建两个按钮,将它们连接到单元格子类中的一个操作,然后从中可以看到选择了哪一行。