Rory Byrne
Rory Byrne

Reputation: 923

How can I access a TableViewController's variable in a subclassed TableViewCell file?

I have an array of Friend ID numbers in my TableViewController which I am using to set the title of each cell. In my subclassed TableViewCell, I have a button which deletes the friend associated with the cell. I need to know the Friend ID number associated with the cell in order to make the HTTP call in the TableViewCell code.

How can I access my Friend ID array in the subclassed TableViewCell? Is there a better way of doing this?

Upvotes: 0

Views: 53

Answers (3)

drewag
drewag

Reputation: 94723

You should find a way for the cell to signify to the controller that the button has been pressed and let the view controller (or even better, an object that the view controller creates just for the business logic).

My favorite way to do that is to define a block property on the cell for the button being tapped:

// Custom Cell Header
@property (nonatomic, copy) void(^onButtonTapped)();
@property (nonatomic, strong) NSNumber* friendId;

// Custom Cell Implementation
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.button = [UIButton new];
        [self.button
            addTarget:self
            action:@selector(buttonTapped:)
            forControlEvents:UIControlEventTouchUpInside
        ];

        [self.contentView addSubview:self.button];
    }
    return self;
}

- (void)buttonTapped:(id)sender
{
   if (self.onButtonTapped) {
       self.onButtonTapped(friendId);
   }
}

// Configuring your cell
cell.textLabel.text = @"blah";
cell.friendId = theId;
cell.onButtonTapped = ^(NSNumber *friendId) {
     // Do what you want with the friendId
     // Most likely ask business logic object to do what it should
     // with the friendId
};

Ultimately you want to keep business logic outside of the view for sure, and preferably outside of view controllers because view controllers have a tendency to fill up with code and complexity very quickly.

Upvotes: 1

Marcal
Marcal

Reputation: 1371

Some time ago I asked regarding UIControls inside custom Cells. Check it here:

What I always do is to create a custom object to holds the info for a custom cell. For example I have a custom cell with a button or a textField. On the Custom object I prepare propertie for that specific cell. One of these properties can be a http address linked only to that cell. Another property can be a segueId which is linked only to that cell... you get the idea.

It's confusing in the beginning, but it's a powerful way to create tableViews.

I believe it should work in your case. As I've learned the strategy is to keep track of the indexPath.

Upvotes: 0

dzl
dzl

Reputation: 906

It sounds like you shouldn't be performing that logic in the UITableViewCell, but rather in the controller, or another class altogether.

Upvotes: 1

Related Questions