OshriALM
OshriALM

Reputation: 215

How to pass parameter through UIButton

i have to classes: existUserView and existUserCustomCell.

the code in existUserView:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ExistUserCustomCell *cell = (ExistUserCustomCell*)[tableView dequeueReusableCellWithIdentifier:@"ExistUserCustomCell"];

    KidManager *kid = [self.kidsArray objectAtIndex:indexPath.row];
    cell.kidName.text = kid.firstName;
    if([kid.inside isEqualToString:@"1"]){
        cell.kidStatus. text = @"some string";
    }else{
        cell.kidStatus.text = @"some string";
    }

    return cell;
}

code in existUserCustomCell:

- (IBAction)reportMissing:(id)sender {

}

- (IBAction)callTeacher:(id)sender {

}

i need to pass data from 'existUserView' to both button function in 'existUserCustomCell' and to know the row when i press them. how can i do that the best way?

Upvotes: 0

Views: 55

Answers (1)

rmaddy
rmaddy

Reputation: 318924

If you need to pass data from the view controller to the cell, add a property (or two, or three) to the cell class. Set the property when you setup the cell in cellForRowAtIndexPath. Then you cell's methods (including the button handlers) have access to the data as needed.

Add a property to your cell class:

@interface ExistUserCustomCell : UITableViewCell

// add this to anything you have
@property (nonatomic, strong) KindManager *kid;

@end

Now your button methods have access:

- (IBAction)reportMissing:(id)sender {
    // access self.kid to get data
    // anything else you need
}

Then in the table view controller:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ExistUserCustomCell *cell = (ExistUserCustomCell*)[tableView dequeueReusableCellWithIdentifier:@"ExistUserCustomCell"];

    KidManager *kid = [self.kidsArray objectAtIndex:indexPath.row];
    cell.kid = kid;
    cell.kidName.text = kid.firstName;
    if([kid.inside isEqualToString:@"1"]){
        cell.kidStatus. text = @"some string";
    }else{
        cell.kidStatus.text = @"some string";
    }

    return cell;
}

I'm guessing that it is the KidManager data that you need in the cell. Adjust as needed.

BTW - if my guess is correct then the cell should set itself up with the data instead of having the logic in the cellForRowAtIndexPath method.

Upvotes: 2

Related Questions