0

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?

Rory Byrne
  • 923
  • 1
  • 12
  • 22

3 Answers3

1

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

dzl
  • 908
  • 11
  • 32
  • So I should program the cell's button in the ViewController class? I need to create a custom prototype cell in order to add the button to it in the Storyboard, though? Sorry, I'm a little confused. – Rory Byrne Apr 02 '14 at 16:09
  • 1
    Correct. It sounds like you should have another class responsible for the business logic of managing friends' numbers. @drewag's suggestion of a block property in the button class, along with that, should be a good starting point of moving the logic out of the cell. – dzl Apr 02 '14 at 16:12
1

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.

drewag
  • 93,393
  • 28
  • 139
  • 128
  • What exactly is that piece of code doing? How can I link it to a method in my view controller? I haven't come across block properties yet, so I feel a bit in over my head. edit: I actually haven't learned about Obj-C blocks at all, so I'll have to read up before I can implement this. – Rory Byrne Apr 02 '14 at 16:18
  • I added more to my example code above. I definitely recommend learning about blocks because they can be really great. – drewag Apr 02 '14 at 19:23
0

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.

Community
  • 1
  • 1
Marcal
  • 1,371
  • 5
  • 19
  • 37