在表格视图中使用手动按钮

时间:2013-09-05 11:29:49

标签: ios objective-c

我正在创建一个评论应用程序,它在数组的表视图中显示许多注释。用户可以喜欢,不喜欢或标记评论。我已将此合并到应用程序中,但是,只要用户点击了喜欢,不喜欢或标记,就会始终对第一条评论进行操作。

这是我的cellForRowAtIndexPath中的前几行:

NSDictionary *myArray = [commentArray objectAtIndex:indexPath.row];
commentID = [myArray objectForKey:@"ID"];

我尝试将commentID作为按钮的标记发送,但我意识到这是一个包含许多字母的ID,例如7c3769f28c9547f4b6889201a8c13f1e。

任何帮助将不胜感激。感谢

3 个答案:

答案 0 :(得分:2)

最简单的方法是在创建按钮时将按钮的标记设置为单元格的indexPath.row

然后在您的likeButtonPressed:和其他按钮处理程序方法中,您可以使用按钮的标记从正确的索引中获取数据:

-(void)likeButtonPressed:(id)sender
{
    UIButton *button = sender;
    NSDictionary *commentData = commentArray[button.tag];
    // Do what you want with commentData here...
}

答案 1 :(得分:0)

我这样在我的应用中实现了相同的概念, 创建一个nsobject类,声明并实现按钮,在你的类单元格中为行方法写这样的方式, cell =(customCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; cell = [[DetailsRoomsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; cell.buttonRoomType1.tag = indexPath.row; [cell.button1 addTarget:self action:@selector(aMethod1 :) forControlEvents:UIControlEventTouchDown]; cell.button2.tag = indexPath.row + 1; [cell.buttonRoomType2 addTarget:self action:@selector(aMethod2 :) forControlEvents:UIControlEventTouchDown];

这会对你有帮助。

答案 2 :(得分:0)

您可以使用此代码:

cellForRowAtIndexPath代码

btnlike.tag = indexPath.row
[btnlike addTarget:self action:@selector(likeButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

likeButtonPressed Method

-(IBAction)likeButtonPressed:(id)sender{
NSLog(@"Button tag :%d",[sender tag]);
}