UITableview在删除行

时间:2015-05-10 13:51:13

标签: ios objective-c uitableview reloaddata

我有一个UITableview,从后端获取“课程”数据。用户可以接受或拒绝单元格上有2个uibutton的课程。

问题是如果用户拒绝多个课程,该按钮似乎没有更新并拒绝错误的课程。

以下是我添加我的uibutton的方法:

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"userWaitingCell" forIndexPath:indexPath];

        UIButton *acceptBtn = (UIButton *)[cell viewWithTag:200];
        acceptBtn.tag = [[dic objectForKey:@"id"] integerValue] + 10000;
        [acceptBtn addTarget:self action:@selector(acceptLesson:) forControlEvents:UIControlEventTouchUpInside];

        UIButton *refuseBtn = (UIButton *)[cell viewWithTag:201];
        refuseBtn.tag = [[dic objectForKey:@"id"] integerValue] + 10000;
        [refuseBtn addTarget:self action:@selector(refuseLesson:) forControlEvents:UIControlEventTouchUpInside];

        NSLog(@"Btn TAG: %ld", (long)refuseBtn.tag);

        return cell;

我使用后端数据库中的课程的唯一ID设置标记。

以下是我如何处理用户点按按钮的方法:

- (void)refuseLesson:(id)sender
{
   UIButton *senderBtn = (UIButton *)sender;
   NSString *tmpUrl = [NSString stringWithFormat:@"%@%ld",
                      @"lesson/refuse/", senderBtn.tag - 10000];
   NSLog(@"tmpUrL : %@", tmpUrl);
   [dbService sendPostRequest:tmpUrl];
}

我在NSLog中可以看到这是错误的课程ID。

当我完成请求时,我正在设置我的数据数组:

    for (int i = 0; i < [lessonList count]; i++)
{
    NSMutableDictionary *lesson = [lessonList objectAtIndex:i];
    if ([[lesson objectForKey:@"id"] integerValue] == [[dataDic objectForKey:@"id"] integerValue])
    {
        if ([[dataDic objectForKey:@"invitationStatus"] isEqualToString:@"accepted"])
        {
            [lesson setObject:@"accepted" forKey:@"invitationStatus"];
            [lesson setObject:[dataDic objectForKey:@"lesson_count"] forKey:@"lesson_count"];
            [lesson setObject:[dataDic objectForKey:@"lesson_done"] forKey:@"lesson_done"];
        }
        else if ([[dataDic objectForKey:@"invitationStatus"] isEqualToString:@"refused"])
        {
            [lesson setObject:@"refused" forKey:@"invitationStatus"];
            [lessonList removeObjectAtIndex:i];
        }
        [self.myTableView reloadData];
        NSLog(@"RELOADING");
        return ;
    }
}

0 个答案:

没有答案