2017-06-03 42 views
0

當按下Delete鍵將刪除選定的從Web服務器數據源,但無法加載的TableView對象,之後刪除行,無法重新從NSObject的UITableView的數據文件

這裏是我的代碼,請指點是如何做到這一點或讓 我知道我在做這個錯誤。

- (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]; 


} 

請不要讓我知道如果你需要更多的信息,是爲目標文件或數據檢索代碼片段從服務器端獲取數據的代碼。

回答

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]; 
} 
相關問題