Azy Qadir
Azy Qadir

Reputation: 155

how to delete row in uitableview when click on button

hi i am implementing a table in which i am getting request i have list of students to which i want to add as my room mate i have two button accept and reject the problem is here when i click on accept button i want that row will be deleted from the array and also i want to shift the deleted row value into the table of nextview controller in case of accept request . and in case of reject button cliked only want to delete the row not to move the value of cell into next view controller i am implementing this project by using storyboard can anybody have idea about that plase share me this is the code for cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DefaultCell"];
    if(cell == nil) {
        cell =[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"DefaultCell"];

    }
    acceptreq = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    acceptreq.frame =CGRectMake(170, 10, 60, 30);
    rejectreq = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    rejectreq.frame =CGRectMake(240, 10, 60, 30);
    [acceptreq setTitle:@"Accept" forState:UIControlStateNormal];
    [rejectreq setTitle:@"Reject" forState:UIControlStateNormal];
    [acceptreq addTarget:self action:@selector(Acceptrequest:) forControlEvents:UIControlEventTouchUpInside];
    [rejectreq addTarget:self action:@selector(Rejectrequest:) forControlEvents:UIControlEventTouchUpInside];
    [cell addSubview:acceptreq];
    [cell addSubview:rejectreq];
    [acceptreq setTag:indexPath.row];
    cell.textLabel.text = [myarray objectAtIndex:indexPath.row];
    return cell;

}

Upvotes: 0

Views: 3642

Answers (2)

Nitin Gohel
Nitin Gohel

Reputation: 49730

In short you can delete selected Row with clicking UIbutton like bellow:-

-(IBAction)Acceptrequest:(id)sender
{
UIButton *btn =(UIButton*)sender;

[myarray removeObjectsAtIndexes:btn.tag];
[tableView deleteRowsAtIndexPaths:myarray withRowAnimation:UITableViewRowAnimationAutomatic];

[tableVIew reloadData];
}

Upvotes: 1

pbibergal
pbibergal

Reputation: 2921

You need to implement this method:

[tableView deleteRowsAtIndexPaths:arrayOfIndexPathsToDelete withRowAnimation:UITableViewRowAnimationAutomatic];

Upvotes: 0

Related Questions