lhencq
lhencq

Reputation: 505

How to get value from UITableViewCell

I'm having a problem in getting a value in uitableviewcell.

first of all, i have a label on my cell which I've created a tap gesture on it. What i want to do is on tapping that label, viewUser method will be call and it will get the details of that cell being tapped.

Here is my code:

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;

Now, when i call my ontap method which is 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 = @"[email protected]";
[self.navigationController pushViewController:chkDtl animated:YES];
}

Please help me on this.

Upvotes: 2

Views: 912

Answers (4)

Marko Zadravec
Marko Zadravec

Reputation: 8740

When creating UILable you could put the tag value as row number

lable.tag = indexPath.row

In your method then retrieve label and look for tag value.

Upvotes: 1

Julian F. Weinert
Julian F. Weinert

Reputation: 7570

Definitely you should use the proper delegate methods instead implementing UITapGestureRecognizer on an UITableViewCell. Then you can get the informations you want pretty easily:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *theUserNameYouWantToGet = [_workflowList objectAtIndex:[indexPath row]];
}

EDIT:

If you want that action not to work for the whole cell, you could subclass UITableViewCell, add properties to it and implement your own delegate protocol to that cell:

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

Then use this in your 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];

  }

Upvotes: 0

dispatchMain
dispatchMain

Reputation: 1617

The best option would be to use UITableView delegate method tableView:didSelectRowAtIndexpath: as below:

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

Don't forget to set delegate of your tableView.

UPDATE: Then I think you want to do something like below. In your cellForRowAtaIndexPath: add following:

cell.userNameLabel.tag=indexPath.row;

And in viewUser method:

UITapGestureRecognizer *tap=(UITapGestureRecognizer*)sender;
WorkflowProfileViewController *chkDtl = [[WorkflowProfileViewController alloc]init];
chkDtl.name=[[_workflowList objectAtIndex:tap.view.tag] userName];
[self.navigationController pushViewController:chkDtl animated:YES];

Upvotes: 1

David Karlsson
David Karlsson

Reputation: 9716

Something like below will get the cell you are tapping (replace swipe with tap):

- (void)handleSwipeRight:(UISwipeGestureRecognizer *)gestureRecognizer{
    CGPoint location = [gestureRecognizer locationInView:self.tableView];
    NSIndexPath *swipedIndexPath = [self.tableView indexPathForRowAtPoint:location];
    self.currentSwipedCell  = [self.tableView cellForRowAtIndexPath:swipedIndexPath];

 }

Havent tested it yet but:

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

Upvotes: 0

Related Questions