我创建了一个带有自己的 .m、.h 和 .xib 文件的自定义单元格。在单元格中,我有一个 UIButton,已添加到 IB 的 xib 中。
我可以在此自定义单元格的 .m 中从 UIButton 接收 IBAction,但实际上,我想将该按钮按下转发到托管表格(以及自定义单元格)的主视图 .m 并在那里使用操作.
在过去的 4 个小时里,我一直在尝试各种方法——我应该使用 NSNotificationCenter 吗?(我已经尝试了很多通知,但无法让它工作,并且不确定我是否应该坚持下去)
我创建了一个带有自己的 .m、.h 和 .xib 文件的自定义单元格。在单元格中,我有一个 UIButton,已添加到 IB 的 xib 中。
我可以在此自定义单元格的 .m 中从 UIButton 接收 IBAction,但实际上,我想将该按钮按下转发到托管表格(以及自定义单元格)的主视图 .m 并在那里使用操作.
在过去的 4 个小时里,我一直在尝试各种方法——我应该使用 NSNotificationCenter 吗?(我已经尝试了很多通知,但无法让它工作,并且不确定我是否应该坚持下去)
您需要在单元格的 .h 文件中使用委托。像这样声明委托
@class MyCustomCell;
@protocol MyCustomCellDelegate
- (void) customCell:(MyCustomCell *)cell button1Pressed:(UIButton *)btn;
@end
然后声明字段和属性
@interface MyCustomCell:UItableViewCell {
    id<MyCustomCellDelegate> delegate;
}
@property (nonatomic, assign) id<MyCustomCellDelegate> delegate;
@end
在 .m 文件中
@synthesize delegate;
并在按钮方法中
- (void) buttonPressed {
    if (delegate && [delegate respondToSelector:@selector(customCell: button1Pressed:)]) {
        [delegate customCell:self button1Pressed:button];
    }
}
你的视图控制器必须像这样采用这个协议
.h 文件
#import "MyCustomCell.h"
@interface MyViewController:UIViewController <MyCustomCellDelegate>
.....
.....
@end
在 cellForRow 的 .m 文件中:您需要将属性委托添加到单元格的方法
cell.delegate = self;
最后你从协议中实现方法
- (void) customCell:(MyCustomCell *)cell button1Pressed:(UIButton *)btn {
}
对不起我的英语和代码。从我的电脑上写的,没有 XCODE
我遇到了同样的问题,我通过将单元格子类化到它自己的类中来解决它,并将按钮作为插座放在那里,并用模型中的数据填充单元格,同时使用返回我们当前正在查看的单元格的方法.
例如,如果您有一个 Person 类,并且每个人都有一个名字、一个姓氏和一些朋友。每次单击单元格中的按钮时,特定人的朋友数量就会增加 1。
_______________DATA SOURCE___________________________
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *comment;
@property (nonatomic) NSInteger numberOfFriends;
+(instancetype)personWithName:(NSString *)aName Surname:(NSString *)aSurname;
@end
#import "Person.h"
@implementation Person
+(instancetype)personWithName:(NSString *)aName Surname:(NSString *)aSurname{
    Person *person = [[Person alloc] init];
    [person setName:aName];
    [person setSurname:aSurname];
    [person setNumberOfFriends:0];
    return person;
}
@end
_____________________PERSON CELL________________________
#import <UIKit/UIKit.h>
@interface PersonCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *friendsNum;
@property (strong, nonatomic) IBOutlet UIButton *friendsBtn;
@property (strong, nonatomic) IBOutlet UILabel *nameLabel;
@property (strong, nonatomic) IBOutlet UILabel *surnameLabel;
@end
我个人创建了一个私有 NSArray 来保存我的 Person 对象的名称,以及一个私有 NSMutableDictionary 来保存我的 Person 对象,并且我将键设置为人名。
_____________________PERSON TABLE VIEW________________________    
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    PersonCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    NSString *name = [peopleNames objectAtIndex:indexPath.row];
    Person *person = [people objectForKey:name];
    if(cell == nil)
    {
        cell = [[PersonCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    // Configure the cell...
    cell.nameLabel.text = person.name;
    cell.surname.Label.text = person.surname
    [cell.friendsButton addTarget:self action:@selector(moreFriends:) forControlEvents:UIControlEventTouchUpInside];
    cell.friendsNum.text = [NSString stringWithFormat:@"%i", person.numberOfFriends];
    return cell;
}
- (IBAction)moreFriends:(id)sender {
    UIButton *btn = (UIButton *)sender;
    PersonCell *cell = [self parentCellForView:btn];
    Person *person = [people objectForKey:cell.nameLabel.text];
    person.numberOfFriends++;
    [self.tableView reloadData];
}
-(PersonCell *)parentCellForView:(id)theView
{
    id viewSuperView = [theView superview];
    while (viewSuperView != nil) {
        if ([viewSuperView isKindOfClass:[PersonCell class]]) {
            return (PersonCell *)viewSuperView;
        }
        else {
            viewSuperView = [viewSuperView superview];
        }
    }
    return nil;
}
我建议使用委托。
您可能只想为具有您的表格视图的视图控制器中的按钮添加一个选择器。
在你的 cellForIndexPath 函数中
[yourCell.button addTarget:self action:@selector(customActionPressed:) forControlEvents:UIControlEventTouchDown];
然后在“customActionPressed:(id) sender”方法中处理按钮按下
    //Get the superview from this button which will be our cell
UITableViewCell *owningCell = (UITableViewCell*)[sender superview];
//From the cell get its index path.
NSIndexPath *pathToCell = [myTableView indexPathForCell:owningCell];
    //Do something with our path
这对您来说可能是一个更好的解决方案,除非有更多您没有列出的因素。
我有一个教程可能会解释更多http://www.roostersoftstudios.com/2011/05/01/iphone-custom-button-within-a-uitableviewcell/
为什么不为您的自定义单元格创建一个委托(使用@protocol)。然后,您可以将主视图指定为每个单元格的委托并适当地处理操作。