I would like to delete cell by one swipe gesture. The problem is that when I slide left delete icon will appear. Of course when I click on delete icon cell will be deleted. I would like to delete cell immediately after swipe gesture. It is supported by ios 9?
More details
When the user slide left to the middle of the cell delete button will appear. When he will continue swiping to the edge of the screen cell will be deleted.
Why don't you have a custom table cell
On the cell add swipe gesture on the content view or any portion,
Delegate its call to the tableviewcontroller with the indexpath
- (void)someDelegateFunctionToDeleteCellAtIndexPath:(NSIndexpath *)indexPath{
[dataSourceArray removeObjectAtIndex:indexPath.row];
NSArray *deleteIndexPaths = #[indexPath];
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
}
Use UISwipeGesture on UITableView:
- (void)viewDidLoad
{
[super viewDidLoad];
UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:sel
action:#selector(leftSwipe:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[self.tableView addGestureRecognizer:recognizer];
}
- (void)leftSwipe:(UISwipeGestureRecognizer *)gestureRecognizer
{
CGPoint location = [gestureRecognizer locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
[dataSourceArray removeObjectAtIndex:indexPath.row];
[tableView reloadData];
}
I tried the solution for your question.Very easily I got the solution.
.m
#import "ViewController.h"
#interface ViewController ()
{
NSMutableArray *arrayData;
}
#end
#implementation ViewController
#synthesize tableViewSwipeDelete;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
arrayData = [[NSMutableArray alloc]initWithObjects:#"iOS",#"Android",#"Windows",#"Tablet",#"iPAD", nil];
UISwipeGestureRecognizer *gestureDeleteRow = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(cellSwipe:)];
gestureDeleteRow.direction = UISwipeGestureRecognizerDirectionLeft;
[tableViewSwipeDelete addGestureRecognizer:gestureDeleteRow];
}
-(void)cellSwipe:(UISwipeGestureRecognizer *)gesture
{
CGPoint location = [gesture locationInView:tableViewSwipeDelete];
NSIndexPath *swipedIndexPath = [tableViewSwipeDelete indexPathForRowAtPoint:location];
//Delete Row…
[arrayData removeObjectAtIndex:swipedIndexPath.row];
[tableViewSwipeDelete deleteRowsAtIndexPaths:[NSArray arrayWithObjects:swipedIndexPath, nil] withRowAnimation:UITableViewRowAnimationLeft];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return arrayData.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *strCell = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strCell];
if(cell==nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strCell];
}
cell.textLabel.text = arrayData[indexPath.row];
return cell;
}
#end
Above my answer works perfectly.
Related
I have a UITableView populated with cells that have buttons on them. I want those buttons to work also when in editing mode, but they don't. It seems like the gesture recognizer on UITableViewCell is preventing gesture recognizers on buttons. Does anyone have any suggestions about approaching this problem?
ViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
self.items = [#[#"one", #"two", #"three", #"four"] mutableCopy];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: #"cellID"
forIndexPath:indexPath];
if (cell == nil) {
cell = [[TableViewCell alloc] init];
}
[cell.button setTitle: self.items[indexPath.row]
forState: UIControlStateNormal];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.items removeObjectAtIndex: indexPath.row];
}
}
TableViewCell.h:
#interface TableViewCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UIButton *button;
- (IBAction)buttonTapped:(id)sender;
#end
So, the buttonTapped: is called when a cell isn't in editing mode, but when a cell is in editing mode, the button doesn't work.
Use different tags from each button in the cells. For example,
Inside cellforRowAtIndexPath, specify a tag for a button.
[Button addTarget:self action:#selector(func:event:) forControlEvents:UIControlEventTouchUpInside];
[Button setTag:indexPath.row];
Now call the function and refer the cell using tag.
-(IBAction)func:(id)sender event:(id)event{
UITouch *touch = [[event allTouches] anyObject];
CGPoint Pos = [touch locationInView:self.tableview];
NSIndexPath *indexPath = [self.tableview indexPathForRowAtPoint:Pos];
if(indexPath != nil){
//do operation with indexPath
}
}
Hope it helps...
First of all see some code what you have done !! without proper declaration its difficult to give answer.
Firstly My suggestion is give gesture recognize in UItableview delegate method.
- (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
// code
}
Sometimes gesture is not working with small mistake in coding.
Secondly if possible ,
Instead of adding the Gesture recognizer to the Cell directly, you can add it to the UItableview in viewDidLoad.
This is the best way to work gesture in UITableview.
As said by Phix in Example,
In the didSwipe-Method you can determine the affected IndexPath and cell as follows:
-(void)didSwipe:(UIGestureRecognizer *)gestureRecognizer {
if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
CGPoint swipeLocation = [gestureRecognizer locationInView:self.tableView];
NSIndexPath *swipedIndexPath = [self.tableView indexPathForRowAtPoint:swipeLocation];
UITableViewCell* swipedCell = [self.tableView cellForRowAtIndexPath:swipedIndexPath];
// ...
}
}
You can set other gesture like as Above.
I hope it will solve your issue.
To do this, you need to subclass the UITableView and make it conform to a UIGestureRecognizerDelegate protocol. In .m file of your UITableView subclass add the following code:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldReceiveTouch:(UITouch *)touch {
if ([touch.view isKindOfClass: [UIButton class]]) {
return NO;
}
return YES;
}
In ViewDidLoad of table view controller, I first added SwipeGuesture and handler "Swipecell"
UISwipeGestureRecognizer *showExtrasSwipe = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:#selector(Swipecell:)];
showExtrasSwipe.direction = UISwipeGestureRecognizerDirectionRight|
UISwipeGestureRecognizerDirectionLeft;
[self.tableView addGestureRecognizer:showExtrasSwipe];
//Here I define the handler:
-(void)Swipecell:(UISwipeGestureRecognizer *)gesture
{
//Taking gesture location
CGPoint location = [gesture locationInView:_tableview];
//Getting IndexPath for that location in tableview
NSIndexPath *swipedIndexPath = [self.tableView indexPathForRowAtPoint:location];
UITableViewCell *swipedCell = [self.tableView cellForRowAtIndexPath:swipedIndexPath];
NSString *data = [self.dataArray objectAtIndex:swipedIndexPath.row];
//Setting the label of the cell
[swipedCell.textLabel setText:data];
}
But it is working only for one first row. Can anybody help me in this or give me better solution? Don't forget to rate, if you like the question.
Please add the swipe gesture in cellForRowAtIndexPath method , so the gesture will be added to each cell.
It works... Tested on iOS 7.1 simulator
- (void)viewDidLoad
{
_tableview = (UITableView*)self.view;
[super viewDidLoad];
UISwipeGestureRecognizer *showExtrasSwipe = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:#selector(Swipecell:)];
showExtrasSwipe.direction = UISwipeGestureRecognizerDirectionRight|
UISwipeGestureRecognizerDirectionLeft;
[self.tableView addGestureRecognizer:showExtrasSwipe];
}
-(void)Swipecell:(UISwipeGestureRecognizer *)gesture
{
CGPoint location = [gesture locationInView:_tableview];
NSIndexPath *swipedIndexPath = [self.tableView indexPathForRowAtPoint:location];
UITableViewCell *swipedCell = [self.tableView cellForRowAtIndexPath:swipedIndexPath];
swipedCell.detailTextLabel.text = [NSString stringWithFormat:#"%i", swipedIndexPath.row];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 55;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"cell"];
}
cell.textLabel.text = [NSString stringWithFormat:#"%i", indexPath.row];
cell.detailTextLabel.text = #" ";
return cell;
}
Change [gesture locationInView:_tableview] to [gesture locationInView:self.tableview]. I was refering to tableView outlet.
I'm still learning iOS and this is my first major project, so go easy on me. I'm trying to make a gesture-driven task list. I still have a lot of work to do, but I hope you guys can help me get this working. I'm certain I set the delegate and data source properly, but it's still not showing up...anyway, here's my ViewController.m file.
Thank you in advance!!
#import "AGViewController.h"
#import "AGTaskObject.h"
#import "AGTableViewCell.h"
//#import "AGSettingsViewController.h"
#interface AGViewController ()
#end
#implementation AGViewController {
NSMutableArray *taskItems;
}
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
//Initializing the Array
taskItems = [[NSMutableArray alloc] init];
[taskItems addObject:[AGTaskObject toDoItemWithText:#"Tap the + to add a Task"]];
[taskItems addObject:[AGTaskObject toDoItemWithText:#"Tap the Gear Button to check out settings"]];
[taskItems addObject:[AGTaskObject toDoItemWithText:#"Swipe Right to check off a task"]];
[taskItems addObject:[AGTaskObject toDoItemWithText:#"Swipe Left to Delete a Task"]];
[taskItems addObject:[AGTaskObject toDoItemWithText:#"I <3 Bacon"]];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//Table View Delegates
self.tableView.delegate = self;
self.tableView.dataSource = self;
//set the initial background image
self.tableView.backgroundColor = [UIColor clearColor];
self.backgroundImage.image = [UIImage imageNamed:#"blue.png"];
[self.tableView registerClass:[AGTableViewCell class] forCellReuseIdentifier:#"cell"];
//Get rid of empty cells at bottom of tableview
// self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
// Long Press Gesture to rearrange cells
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPressGestureRecognized:)];
[self.tableView addGestureRecognizer:longPress];
// // Tap Gesture to add a new cell
// UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(<#selector#>)];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Button Press (IB)Actions
- (IBAction)settingsButtonPressed:(UIButton *)sender
{
[self performSegueWithIdentifier:#"toSettingsViewController" sender:nil];
}
- (IBAction)addTaskButtonPressed:(UIButton *)sender
{
}
#pragma mark - UITableView Configuration
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [taskItems count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"cell";
//re-Use or create a tableview cell
AGTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier
forIndexPath:indexPath];
/* if (cell == nil) {
cell = [[AGTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
*/
//Make the cells clear to show the background image
cell.backgroundColor = [UIColor clearColor];
//default font color will be white
cell.textLabel.textColor = [UIColor whiteColor];
//Set the task for each cell in the tableview
AGTaskObject *item = taskItems[indexPath.row];
//set the text
cell.delegate = self;
cell.taskObject = item;
// Recall final cell
return cell;
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [self colorForIndex:indexPath.row];
}
#pragma mark - TableItem Delete
-(void)toDoItemDeleted:(id)taskobject {
// use the UITableView to animate the removal of this row
NSUInteger index = [taskItems indexOfObject:taskobject];
[self.tableView beginUpdates];
[taskItems removeObject:taskobject];
[self.tableView deleteRowsAtIndexPaths:#[[NSIndexPath indexPathForRow:index inSection:0]]
withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
}
Storyboards don't use initWithNibName (even though Xcode creates it pointlessly) so your array is empty. Try moving it to viewDidLoad
I'm making an application with a tableview list of contacts that can be reached via a tab-controller at the bottom.
I copied (literally copy/pasted) from the example Master Detail Application and tried to make sure all storyboard references lined up.
#import "ContactsTableViewController.h"
#import "ContactViewController.h"
#interface ContactsTableViewController () {
NSMutableArray *_objects;
}
#end
#implementation ContactsTableViewController
- (void)awakeFromNib
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
self.clearsSelectionOnViewWillAppear = NO;
self.preferredContentSize = CGSizeMake(320.0, 600.0);
}
[super awakeFromNib];
// [self.tableView setDelegate:self]; // From (unsuccesfully) trying https://stackoverflow.com/questions/16311393/how-to-insert-items-to-a-uitableview-when-a-uibutton-is-clicked-in-ios
// [self.tableView setDataSource:self];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;
self.contactViewController = (ContactViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)insertNewObject:(id)sender
{
if (!_objects) {
_objects = [[NSMutableArray alloc] init];
}
[_objects insertObject:[NSDate date] atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; // Crashes here
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _objects.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath]; // Crashes here
NSDate *object = _objects[indexPath.row];
cell.textLabel.text = [object description];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[_objects removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
NSDate *object = _objects[indexPath.row];
self.contactViewController.detailItem = object;
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSDate *object = _objects[indexPath.row];
[[segue destinationViewController] setDetailItem:object];
}
}
#end
It crashes on the first line in CellRowAtIndexPath. Since I was having trouble I also took the advice in How to insert items to a UITableView when a UIButton is clicked in iOS but it didn't solve my problem.
This is just incredibly frustrating, because as far as I can tell my code is (except for names) exactly the same as the example application.
edit: Exception message is
'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
Alex - When using storyboards and the new prototype cell feature in xCode, you have to set an identifier value in Interface Builder whose value should match what is in your code.
So notice you have this line:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
In this case, your cell identifier is "Cell"
Can you confirm that in Interface Builder/Storyboard, your table view cell's identifier is set to the same?
As an example, here's a screenshot from a sample app I was building (Notice my Indentifier field on the right):
I would recommend this
-(void)ViewDidLoad{
[tableView registerClass:[MyCell class] forCellReuseIdentifier:#"Cell"];
}
- (void)insertNewObject:(id)sender
{
if (!_objects) {
_objects = [[NSMutableArray alloc] init];
}
[_objects insertObject:[NSDate date] atIndex:0];
[tableView reloadData]; //this will trigger cellForRowAtIndexPath again with the updated array
}
and would you please post your error.
You should not use [self.tableView insertRowsAtIndexPaths: withRowAnimation:];. Instead of this you should call [talbeView reloadData] method.
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 = #"romelync#sxchange.com";
[self.navigationController pushViewController:chkDtl animated:YES];
}
Please help me on this.
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];
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.
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
}
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];
}