删除行后,无法从NSObject文件重新加载UITableView数据

时间:2017-06-03 09:50:38

标签: ios objective-c uitableview nsmutablearray nsobject

按“删除”按钮时,它会从WebServer数据源中删除所选对象,但无法重新加载TableView,

  

这是我的代码,请建议如何完成此操作或让   我知道我在做错了什么。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell" forIndexPath:indexPath];

    Records * RecordsObject;

    RecordsObject = [recordsArray objectAtIndex:indexPath.row];
    cell.textLabel.text = RecordsObject.recordsMname;
    cell.detailTextLabel.text= RecordsObject.recordsImei;

    UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [detailButton setImage:[UIImage imageNamed:@"edit"] forState:UIControlStateNormal];
    detailButton.frame = CGRectMake(WidthCalculation - 45, 0.0f, 50.0f, 45.0f);
    [cell addSubview:detailButton];
    [detailButton addTarget:self
                     action:@selector(detailsButton:)
           forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:detailButton];
    [detailButton setTag:indexPath.row];


    UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [deleteButton setImage:[UIImage imageNamed:@"trash"] forState:UIControlStateNormal];
    deleteButton.frame = CGRectMake(WidthCalculation - 75, 0.0f, 50.0f, 45.0f);
    [cell addSubview:deleteButton];
    [deleteButton addTarget:self
                       action:@selector(delButton:event:)
             forControlEvents:UIControlEventTouchUpInside];
     [cell.contentView addSubview:deleteButton];



    // Configure the cell...

    return cell;
}

删除按钮功能

- (IBAction)delButton: (UIButton*)button event:(UIEvent*)event
{
    UIButton *delButton = button;
    NSLog(@"FaceBook Button is sharing %li row's Image", (long)delButton.tag);

    NSIndexPath* indexPath = [self.tableView indexPathForRowAtPoint:
                              [[[event touchesForView:button] anyObject]
                               locationInView:self.tableView]];

    Records *object = self.recordsArray[delButton.tag];
    NSLog(@"selected Object %@", object);
    NSString *id = object.recordsId;
    NSMutableString * postString = [NSMutableString stringWithString:kdeleteURL];
    [postString appendString:[NSString stringWithFormat:@"?%@=%@", kId, id]];
    [postString setString:[postString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]]];

    NSMutableURLRequest *request =[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:postString]];

    [request setHTTPMethod:@"POST"];

    postConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
    [self.recordsArray removeObjectAtIndex:indexPath.row];

            [self.tableView reloadData];


}

如果您需要更多信息,请告知我们,例如用于从服务器端检索数据的目标文件或数据检索代码段的代码。

1 个答案:

答案 0 :(得分:0)

NSIndexPath *index = [[NSIndexPath alloc] init];

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell" forIndexPath:indexPath];

    Records * RecordsObject;

    RecordsObject = [recordsArray objectAtIndex:indexPath.row];
    cell.textLabel.text = RecordsObject.recordsMname;
    cell.detailTextLabel.text= RecordsObject.recordsImei;

    UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [detailButton setImage:[UIImage imageNamed:@"edit"] forState:UIControlStateNormal];
    detailButton.frame = CGRectMake(WidthCalculation - 45, 0.0f, 50.0f, 45.0f);
    [cell addSubview:detailButton];
    [detailButton addTarget:self
                     action:@selector(detailsButton:)
           forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:detailButton];
    [detailButton setTag:indexPath.row];


    UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [deleteButton setImage:[UIImage imageNamed:@"trash"] forState:UIControlStateNormal];
    deleteButton.frame = CGRectMake(WidthCalculation - 75, 0.0f, 50.0f, 45.0f);
    deleteButton.tag = indexPath.row
    [cell addSubview:deleteButton];
    [deleteButton addTarget:self
                       action:@selector(delButton:event:)
             forControlEvents:UIControlEventTouchUpInside];
     [cell.contentView addSubview:deleteButton];



    // Configure the cell...

    return cell;
}

- (IBAction)delButton: (UIButton*)button event:(UIEvent*)event
{
    UIButton *delButton = button;
    NSLog(@"FaceBook Button is sharing %li row's Image", (long)delButton.tag);

    NSIndexPath* indexPath = [self.tableView indexPathForRowAtPoint:
                              [[[event touchesForView:button] anyObject]
                               locationInView:self.tableView]];

    Records *object = self.recordsArray[delButton.tag];
    NSLog(@"selected Object %@", object);
    NSString *id = object.recordsId;
    NSMutableString * postString = [NSMutableString stringWithString:kdeleteURL];
    [postString appendString:[NSString stringWithFormat:@"?%@=%@", kId, id]];
    [postString setString:[postString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]]];

    NSMutableURLRequest *request =[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:postString]];

    [request setHTTPMethod:@"POST"];

    postConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
    index = indexPath
}

如果获得成功响应,则从数组中删除行和对象。

-(void)serviceResponce() {
   [self.recordsArray removeObjectAtIndex:indexPath.row];
   [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject: index] withRowAnimation:UITableViewRowAnimationNone];
}