我在uitableviewcell中获取值时遇到问题。
首先,我的单元格上有一个标签,我已在其上创建了一个轻击手势。我想要做的是点击该标签,viewUser方法将被调用,它将获取该单元格的详细信息。
这是我的代码:
on cellForRowAtIndexPath:
cell.userNameLabel.text = [[_workflowList objectAtIndex:indexPath.row] userName];
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewUser:)];
tapGestureRecognizer.numberOfTapsRequired = 1;
[cell.userNameLabel addGestureRecognizer:tapGestureRecognizer];
cell.userNameLabel.userInteractionEnabled = YES;
现在,当我调用我的ontap方法时,它是viewUser:
- (IBAction)viewUser:(id)sender {
//this should not be hard coded
//data should come from cell.
//usernamelabel or i will get the index selected
//and get the details in my array
// like this --> [_workflowList objectAtIndex:index]
WorkflowProfileViewController *chkDtl = [[WorkflowProfileViewController alloc]init];
chkDtl.name = @"USER, USER USER"; ;
chkDtl.phoneNo = @"09173210836";
chkDtl.email = @"romelync@sxchange.com";
[self.navigationController pushViewController:chkDtl animated:YES];
}
请帮我解决这个问题。
答案 0 :(得分:1)
最好的选择是使用UITableView委托方法tableView:didSelectRowAtIndexpath:如下所示:
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
WorkflowProfileViewController *chkDtl = [[WorkflowProfileViewController alloc]init];
chkDtl.name=[[_workflowList objectAtIndex:indexPath.row] userName];
[self.navigationController pushViewController:chkDtl animated:YES];
}
不要忘记设置tableView的委托。
更新:然后我想你想做类似下面的事情。在你的cellForRowAtaIndexPath中:添加以下内容:
cell.userNameLabel.tag=indexPath.row;
在viewUser方法中:
UITapGestureRecognizer *tap=(UITapGestureRecognizer*)sender;
WorkflowProfileViewController *chkDtl = [[WorkflowProfileViewController alloc]init];
chkDtl.name=[[_workflowList objectAtIndex:tap.view.tag] userName];
[self.navigationController pushViewController:chkDtl animated:YES];
答案 1 :(得分:1)
创建UILabel时,您可以将标记值作为行号
lable.tag = indexPath.row
在您的方法中,然后检索标签并查找标记值。
答案 2 :(得分:0)
如下所示将获取您正在点击的单元格(用水龙头替换滑动):
- (void)handleSwipeRight:(UISwipeGestureRecognizer *)gestureRecognizer{
CGPoint location = [gestureRecognizer locationInView:self.tableView];
NSIndexPath *swipedIndexPath = [self.tableView indexPathForRowAtPoint:location];
self.currentSwipedCell = [self.tableView cellForRowAtIndexPath:swipedIndexPath];
}
还没有测试过,但是:
-(void)viewUser:(UITapGestureRecognizer):gestureRecognizer{
CGPoint location = [gestureRecognizer locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
self.currentTappedCell = [self.tableView cellForRowAtIndexPath:indexPath];
NSLog(self.currentTappedCell.name); // If you have a custom cell with a name property
}
答案 3 :(得分:0)
当然,您应该使用正确的委托方法,而不是在UITapGestureRecognizer
上实施UITableViewCell
。然后你可以很容易地得到你想要的信息:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *theUserNameYouWantToGet = [_workflowList objectAtIndex:[indexPath row]];
}
修改强>
如果您希望该操作不适用于整个单元格,您可以继承UITableViewCell
,为其添加属性并为该单元格实现您自己的委托协议:
@protocol JWTableViewCellDelegate;
@interface JWTableViewCell : UITableViewCell
@property (nonatomic, retain) NSString *username;
@property (nonatomic, assign) id<JWTableViewCellDelegate>delegate;
@end
@protocol JWTableViewCell <NSObject>
- (void)tableViewCellDidClickUsername:(JWTableViewCell *)cell;
@end
@implementation JWTableViewCell
- (void)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
[self setupView];
}
return self;
}
// if you use a prototyping NIB
- (void)awakeFromNib {
[self setupView];
}
- (void)setupView {
// add gesture recognizer
}
- (void)handleGesture {
if ([_delegate respondsToSelector:@selector(tableViewCellDidClickUsername:)]) {
[_delegate tableViewCellDidClickUsername:self];
}
}
然后在viewController中使用它:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
JWTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseID" forIndexPath:indexpath];
[cell setUsername:@"Paul"];
[cell setDelegate:self];
}
- (void)tableViewCellDidClickUsername:(JWTableViewCell *)cell {
NSString *theUserNameYouWantToGet = [cell username];
}