我有一个UITableViewCell
包含一个按钮,当用户点击按钮或选择一个单元格时,我需要用两个按钮来呈现一个操作表。这两个按钮都会触发一个循环,并且我将数据(来自单元格)传递给prepareForSegue:
方法。它工作正常,但我想用协议来做,但不幸的是我无法正确实施它。无法实现协议
这是我要做的事现在:
@property (nonatomic, strong) PFUser *userObj;
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ContactsTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"cell"];
PFUser *user = [self.dataSourceArray objectAtIndex:indexPath.row];
cell.usernm.text = user.username;
cell.userId.text = user.objectId;
cell.cellButton.tag = indexPath.row;
[cell.cellButton addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (void)buttonTapped:(id)sender {
UIButton *button = (UIButton *)sender;
self.userObj = [self.dataSourceArray objectAtIndex:button.tag];
[self setupActionSheet];
}
- (void) setupActionSheet {
AHKActionSheet *actionSheet = [[AHKActionSheet alloc] initWithTitle:NSLocalizedString(nil, nil)];
[actionSheet addButtonWithTitle:NSLocalizedString(@"Button 1", nil)
image:[UIImage imageNamed:@"1"]
type:AHKActionSheetButtonTypeDefault
handler:^(AHKActionSheet *as) {
[Utilz checkUser:self.userObj.objectId with:self.userObj.username];
}];
[actionSheet addButtonWithTitle:NSLocalizedString(@"Button 2", nil)
image:[UIImage imageNamed:@"2"]
type:AHKActionSheetButtonTypeDefault
handler:^(AHKActionSheet *as) {
[self performSegueWithIdentifier:@"presentNextView" sender:self];
}];
[actionSheet show];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"presentNextView"]) {
if ([[segue destinationViewController] isKindOfClass:[ViewControllerTwo class]]) {
ViewControllerTwo *dataToPass = [segue destinationViewController];
dataToPass.usrStr = self.usrObj.username;
dataToPass.objStr = self.usrObj.objectId;
}
}
}
这里是我尝试用协议来做到这一点:
首先,我创建了一个叫做协议DataPassProtocol.h
。
然后,我添加一个ENUM到ContactsTableViewCell.h
和进口DataPassProtocol.h
typedef NS_ENUM(NSInteger, MyUserCellAction) { MyUserCellActionCheckUser, MyUserCellActionPerformSegue };
我还添加这些变量到ContactsTableViewCell.h
@property (weak) id <DataPassProtocol> delegate;
@property (nonatomic, strong) PFUser *user;
- (IBAction)buttonTapped:(id)sender;
在ContactsTableViewCell.m
我添加这些方法
- (void)setUser:(PFUser *)user {
_user = user;
// setup UI
self.usernmLabel.text = _user.username;
self.userIdLabel.text = _user.objectId;
}
- (IBAction)buttonTapped:(id)sender {
// cell handles its button being tapped
NSLog(@"Button tapped");
if ([self.delegate respondsToSelector:@selector(myUser:didPerformAction:)]) {
[self.delegate myUser:self.user didPerformAction:MyUserCellActionCheckUser];
}
if ([self.delegate respondsToSelector:@selector(myUser:didPerformAction:)]) {
[self.delegate myUser:self.user didPerformAction:MyUserCellActionPerformSegue];
}
}
这里我在第二条if语句时出错。
Implicit conversion of 'NSInteger' (aka 'int') to 'id' is disallowed with ARC
我不知道为什么会发生这种情况。
另一个错误:
an Expected a type in this method implementation of the protocol file.
- (void)myUser:(PFUser *)user didPerformAction:(MyUserCellAction)action;
此外,在MyDelegateViewController
我得到这样的警告:
Conflicting parameter types in implementation of 'myUser:didPerformAction:': '__strong id' vs 'MyUserCellAction' (aka 'enum MyUserCellAction')
而且我把这个在我的VC在我的表视图。
- (void)myUser:(PFUser *)user didPerformAction:(MyUserCellAction)action {
if (action == MyUserCellActionCheckUser) {
// do what we want
} else if (action == MyUserCellActionPerformSegue) {
// do the other thing we want
}
}
,这里是协议实现:
@protocol DataPassProtocol <NSObject>
@optional
- (void)myUser:(PFUser *)user didPerformAction:(MyUserCellAction)action;
我怎么可能使它工作?这是我的第一个协议,所以我不知道我该怎么做才对。
您可以发布协议吗?看来你的协议声明 – 2015-04-03 21:44:22
@CatalinaT有问题。当然,我已经更新了我的问题。 – rihe 2015-04-03 21:57:32
您可以尝试在'DataProtocol.h'文件中声明枚举,因为这是它应该声明的地方,因为它连接到协议,而不是单元。 – 2015-04-06 20:50:50