Jean-Luc Godard
Jean-Luc Godard

Reputation: 1883

The TableView problem...How to know the row number

what I am doing

I am making a project in which I am using a tableView. In that tableView I have a delete Button in every row..

what I want

I want that When I press the button ...there is a delay of 1 second. and after that the row should be deleted..in that time of delay the delete button of the row that I pressed should be hide.

My Problem

Well I am able to do this thing but there is a problem. When I press the delete button of any row ...only the delete button of the last row hides...

Root of the problem -According to my view
Well I have assigned the delete button in cellForRowAtIndexPath .. and I have assigned the method for it...when I press the button...The method get called..in that method I have made that delete button hidden...

I mean how would it know that In which row that delete button should be hide...

My Question
When I press the delete button how to know in which row that should hide..Now its hiding every time in the Last row only..

Suggestion Pls...

Upvotes: 0

Views: 559

Answers (5)

vrk
vrk

Reputation: 125

to know the row number, you can get it by indexpath.row

Upvotes: 0

Prabh
Prabh

Reputation: 2474

Try this: First set a unique tag to every cell in cellForRowAtIndexPath function:

    [cell setTag:10000+[[userInfo objectForKey:@"id"] intValue]];

After wards, for deletetion use function like this:

-(void)deleteRow:(id)sender{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[sender tag] inSection:0];
[YOURTABLEVIEW deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:0];  

}

Hope this will solve your problem.

Upvotes: 0

Rog
Rog

Reputation: 18670

Here's another option that doesn't rely on tags:

-(void)buttonAction:(id)sender
{
    UIButton *button = (UIButton*)sender;
    UITableViewCell *cell = [[button superview] superview];
    [button setHidden:YES];
    [self.tableView performSelector:@selector(deleteCell:) withObject:cell afterDelay:0.5]

-(void)deleteCell:(UITableViewCell*)cell
{
    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:cell] withRowAnimation:UITableViewRowAnimation{YourChoiceHere}
}

Upvotes: 0

Jhaliya - Praveen Sharma
Jhaliya - Praveen Sharma

Reputation: 31722

Use below in your tableView:cellForRowAtIndexPath method.

[myButton addTarget:self
                             action:@selector(ButtonAction:)
                   forControlEvents:UIControlEventTouchUpInside];

myButton.tag = indexPath.row ;

implement the below method,

-(void) ButtonAction:(id) sender
{
    UIButton* myButton = (UIButton*)sender;
    myButton.hidden = YES;

    //Delete the row at index (myButton.tag)

}

I would suggest you to use custom cell rather then using UITableViewCell directly.

Upvotes: 1

visakh7
visakh7

Reputation: 26390

You can probably try setting tag for each of the delete buttons as the row number and then access the tag in the button click method to know which button was clicked.

Upvotes: 0

Related Questions