【问题标题】:How to get value from UITableViewCell如何从 UITableViewCell 中获取价值
【发布时间】:2013-12-03 09:36:19
【问题描述】:

我在 uitableviewcell 中获取值时遇到问题。

首先,我的单元格上有一个标签,我在其上创建了一个点击手势。我想要做的是在点击该标签时,将调用 viewUser 方法,它将获取被点击的单元格的详细信息。

这是我的代码:

在 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;

现在,当我调用 viewUser 的 ontap 方法时:

- (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];
}

请帮帮我。

【问题讨论】:

  • 你没有。您永远不应该将数据存储在单元格中。您将它存储在一个数组中,然后填充单元格。
  • 什么是部署目标?
  • 是的,如您所见,我所指的数据是我的数组_workflowList 中的数据。我要说的是如何直接从所选单元格中获取它,或者如何获得所选单元格的索引。我想我已经解释得很好了。

标签: ios uitableview


【解决方案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];

【讨论】:

  • 使用 didSelectRowAtIndexPath 不是我的解决方案:(
【解决方案2】:

创建 UILable 时,您可以将标签值作为行号

lable.tag = indexPath.row

然后在您的方法中检索标签并查找标签值。

【讨论】:

  • 嗨,@Ihencq,这个答案对你有帮助吗?如果是,请除了回答,如果不是,请告诉我建议有什么问题。
【解决方案3】:

类似下面的内容将获取您正在点击的单元格(用点击替换滑动):

- (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
}

【讨论】:

  • 但我不能,我不应该刷单元格。它应该只点击标签本身。 :(
  • 这个答案很好,但不是保存单元格,而是使用新的 indexPath 从 self.workflowList 获取信息
  • 我同意马克·莫斯比的观点
【解决方案4】:

当然,您应该使用正确的委托方法,而不是在 UITableViewCell 上实现 UITapGestureRecognizer。然后你可以很容易地得到你想要的信息:

- (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];

  }

【讨论】:

  • 如果他想要“选择单元格”和“选择单元格上的按钮”的不同操作怎么办
  • 它不应该点击整个单元格,只点击标签。实际上我那里还有其他标签,我没有包括它,因为我认为它不再需要了:(
  • 好的,你写了“[...]那个单元格被点击了。”所以我误解了这个问题。要编辑...
猜你喜欢
  • 2017-10-01
  • 2018-04-07
  • 2021-12-26
  • 1970-01-01
  • 2016-08-24
  • 2012-11-29
  • 2018-08-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多