Question

I have an interesting scenario. I have a custom TableViewCell class which contains a button. What I would like to do is catch the touch event of the button inside the custom cell class and throw a new event for the container control(ler) to handle.

Reason for this is that I have a cell with a button marked as "Archive". Now instead of handling the cell code in the custom TableViewController I want to intercept the button call first as I need to hide the archive button and tell the container control (or the TableViewController) to handle an "archive event has occurred on cell X". I don't want the controller to know what goes on inside a cell. That way I have some reusable and cleaner code.

So the controller should be able to receive the event and form the event determine which cell is involved in order to know which record it needs to remove. I can achieve this with the NSNotificationCenter class and have observers take care of it for me but it would be great (I think, don't know if you'll agree) if I can have it raised as an event instead.

Can this be done with iOS development?

I hope my question makes sense.

Thanks

EDIT: It seems that I've been misunderstood. So let me make it clear (unless this isn't possible at all), I do not want the TableViewController to know about the button inside the cell. The Controller needs to receive an event generated by the cell because it caught the original button touch event and from there inside the cell a new event needs to be generated which the controller can catch. Similar to what you can do in the .NET world. However if this cannot be done please let me know. The reason is for cleaner and reusable code.

Was it helpful?

Solution 3

Unless someone can show me a better way, I'm going with this approach.

Say you have a TableViewController:

@interface CustomViewController : UITableViewController<ArchiveItemDelegate> ...

and you have a Custom TableViewCell:

@interface CustomCell : UITableViewCell ...

with the delegate property using the delegate protocol below.

Now I've created a protocol:

@protocol ArchiveItemDelegate <NSObject>

-(void)archiveActionPerformed:(CustomCellData *)cellData onCell:(CustomCell *)cell;

@end

Inside my CustomCell implementation I attach an event listener on the archive button:

- (void)archiveButtonPressed:(UIButton *) sender forEvent:(UIEvent *)event {
    [self.delegate archiveActionPerformed:currentCellData onCell:self];
}

Now in my TableViewController I let it "inherit" from the ArchiveItemDelegate protocol and implement it.

-(void)archiveActionPerformed:(CustomCellData *)cellData onCell:(CustomCell *)cell {
    // do whatever ...
}

Now in my "tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath" method I just attach the TableViewController to the cell.

cell.delegate = self;

Now my TableViewController doesn't know of the internals of the Cell and the cell notifies of an "archive event" when the Archive button is tapped. This might "evolve" a bit but for now I think will do. If someone has a better solution please let me know. You can see I come from the .NET space ;-)

OTHER TIPS

When you create your custom table view cell, you can attach the callback to the button press event. Now you can pass the index as a tag of the button, remove the items from your data source in your uitableview controller.

Reload the tableview and you are good to go.

Hope this helps you.

myCell.archiveButton.tag = indexPath.row ; // This is the index in your data source

[myCell.archiveButton addTarget:self action:@selector(archiveItems:) forControlEvents: UIControlEventTouchUpInside];

void archiveItems:(id)sender {
    int index = sender.tag;
// Remove the item from your data source

[self.myTableview reloadData];

}

Yes, you can do.

First of all you have to set the position as a tag to that button. and then you will set your ViewController a target for the Button, and call a selector method like this.

[yourCustomCell.yourButton setTag:indexPath.row];//set position as tag to remember the position
[yourCustomCell.yourButton addTarget:self action:@selector(doArchive:) forControlEvents:UIControlEventTouchUpInside];

and then in your doArchive method, get the tag, which is actually the position like this:

- (void)doArchive:(id)sender
{    
    int positon = sender.tag;
    // do something with positon and get the data from your Array at that position
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top