Ios Objective-c自定义表格单元格按钮
我正在尝试实现一个表视图,其中所有行都有两个按钮,这些按钮可以处理它们所在索引行的数据 以下是我到目前为止的情况:Ios Objective-c自定义表格单元格按钮,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我正在尝试实现一个表视图,其中所有行都有两个按钮,这些按钮可以处理它们所在索引行的数据 以下是我到目前为止的情况: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"NotificationCell"; NotificationCell *cell =
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"NotificationCell";
NotificationCell *cell = (NotificationCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[NotificationCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NotificationObject *notification = nil;
notification = [_notificationArray objectAtIndex:indexPath.row];
cell.profileImage.image = notification.profileImage;
cell.profileImage.layer.cornerRadius = cell.profileImage.frame.size.height /2;
cell.profileImage.layer.masksToBounds = YES;
cell.profileImage.layer.borderWidth = 0;
cell.detailTextView.text = notification.action;
UIButton *denyButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIButton *acceptButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//set the position of the button
denyButton.frame = CGRectMake(cell.frame.origin.x + 285, cell.frame.origin.y + 20, 23, 23);
[denyButton setBackgroundImage:[UIImage imageNamed:@"DenyRequest.png"] forState:UIControlStateNormal];
[denyButton addTarget:self action:@selector(denyButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
denyButton.backgroundColor= [UIColor clearColor];
[cell.contentView addSubview:denyButton];
acceptButton.frame = CGRectMake(cell.frame.origin.x + 240, cell.frame.origin.y + 20, 23, 23);
[acceptButton setBackgroundImage:[UIImage imageNamed:@"AcceptRequest.png"] forState:UIControlStateNormal];
[acceptButton addTarget:self action:@selector(AcceptButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
acceptButton.backgroundColor= [UIColor clearColor];
[cell.contentView addSubview:acceptButton];
return cell;
}
-(void)denyButtonPressed:(id)sender{
NSLog(@"buttonPressedDeny");
}
-(void)AcceptButtonPressed:(id)sender{
NSLog(@"buttonPressedAccept");
}
但是,我不知道如何找出所选按钮被按下的索引行,以便获得相关数据 最简单的解决方案是为每个按钮分配一个标签。例如:
denyButton.tag = 1000 + indexPath.row;
然后按下DenyButton:
-(void)denyButtonPressed:(id)sender{
UIButton *b = (UIButton *)sender;
NSInteger row = b.tag - 1000;
NSLog(@"buttonPressedDeny: %d", row);
}
变量行将保存按下按钮的索引路径行。添加1000是为了避免与您已有的其他视图发生冲突
让我强调一下,从设计/架构的角度来看,这是最简单的解决方案,但不是最友好的
一个更复杂的解决方案是将按钮作为NotificationCell的一部分,让NotificationCell成为这些按钮的代理,并创建一个协议,允许视图控制器成为每个NotificationCell的代理。然后,当按下按钮时,它将由NotificationCell处理,它将把所需的任何对象传递给视图控制器
例如,在NotificationCell.h中创建以下协议
@protocol NotificationCellDelegate
- (void)denyActionForNotificationObject:(NotificationObject *)notificationObject;
- (void)acceptActionForNotificationObject:(NotificationObject *)notificationObject;
@end
另添加NotificationCell添加属性以保存通知和委托:
@property (nonatomic, strong) NotificationObject *notificationObject;
@property (nonatomic, strong) id<NotificationCellDelegate> delegate;
实现您声明的选择器:
- (void)denyButtonPressed:(id)sender {
if (_delegate) {
[_delegate denyActionForNotificationObject:_notificationObject];
}
}
- (void)AcceptButtonPressed:(id)sender {
if (_delegate) {
[_delegate acceptActionForNotificationObject:_notificationObject];
}
}
然后在视图控制器的cellForRowAtIndexPath中添加:
cell.notificationObject = notificationObject;
cell.delegate = self;
此外,在视图控制器中,实现以下协议:
- (void)denyActionForNotificationObject:(NotificationObject *)notificationObject {
// Do something with the notification object
}
- (void)acceptActionForNotificationObject:(NotificationObject *)notificationObject {
// Do something with the notification object
}
我没有在XCode中对此进行测试,如果它没有编译,我深表歉意。最简单的解决方案是为每个按钮分配一个标记。例如:
denyButton.tag = 1000 + indexPath.row;
然后按下DenyButton:
-(void)denyButtonPressed:(id)sender{
UIButton *b = (UIButton *)sender;
NSInteger row = b.tag - 1000;
NSLog(@"buttonPressedDeny: %d", row);
}
变量行将保存按下按钮的索引路径行。添加1000是为了避免与您已有的其他视图发生冲突
让我强调一下,从设计/架构的角度来看,这是最简单的解决方案,但不是最友好的
一个更复杂的解决方案是将按钮作为NotificationCell的一部分,让NotificationCell成为这些按钮的代理,并创建一个协议,允许视图控制器成为每个NotificationCell的代理。然后,当按下按钮时,它将由NotificationCell处理,它将把所需的任何对象传递给视图控制器
例如,在NotificationCell.h中创建以下协议
@protocol NotificationCellDelegate
- (void)denyActionForNotificationObject:(NotificationObject *)notificationObject;
- (void)acceptActionForNotificationObject:(NotificationObject *)notificationObject;
@end
另添加NotificationCell添加属性以保存通知和委托:
@property (nonatomic, strong) NotificationObject *notificationObject;
@property (nonatomic, strong) id<NotificationCellDelegate> delegate;
实现您声明的选择器:
- (void)denyButtonPressed:(id)sender {
if (_delegate) {
[_delegate denyActionForNotificationObject:_notificationObject];
}
}
- (void)AcceptButtonPressed:(id)sender {
if (_delegate) {
[_delegate acceptActionForNotificationObject:_notificationObject];
}
}
然后在视图控制器的cellForRowAtIndexPath中添加:
cell.notificationObject = notificationObject;
cell.delegate = self;
此外,在视图控制器中,实现以下协议:
- (void)denyActionForNotificationObject:(NotificationObject *)notificationObject {
// Do something with the notification object
}
- (void)acceptActionForNotificationObject:(NotificationObject *)notificationObject {
// Do something with the notification object
}
我没有在XCode中对此进行测试,如果它没有编译,我很抱歉,为什么不在视图层次结构中反向工作,并检查按钮的
超级视图
,它应该是表视图单元格的内容视图。谁的superview
应该是单元格
-(void)denyButtonPressed:(id)sender{
UIButton *button = (UIButton *)sender;
UIView *contentView = button.superview;
UITableViewCell *cell = contentView.superview;
NSIndexPath * indexPath = self.tableView indexPathForCell:cell];
NSLog(@"row containing button: %d", indexPath.row);
}
为什么不向后查看视图层次结构,并检查按钮的
超级视图
,它应该是表视图单元格的内容视图。谁的superview
应该是单元格
-(void)denyButtonPressed:(id)sender{
UIButton *button = (UIButton *)sender;
UIView *contentView = button.superview;
UITableViewCell *cell = contentView.superview;
NSIndexPath * indexPath = self.tableView indexPathForCell:cell];
NSLog(@"row containing button: %d", indexPath.row);
}
如果
tableView
使用单个节,则应将按钮的标记设置为indexPath.row
。然后,在目标方法中,可以使用[sender tag]
创建nsindepath
(或以任何其他方式使用该值)对象。如果tableView
使用单个节,则应将按钮的标记设置为indepath.row
。然后在您的目标方法中,您可以使用[sender tag]
创建一个nsindepath
(或者以您想要的任何其他方式使用该值)对象。这是完美的、惊人的答案这是完美的、惊人的答案