2015-12-08 2 views
0

Как это работает, ячейка выталкивается влево. Кнопка удаления нажата, она обращается к локальной базе данных и удаляет строку с соответствующими данными. Однако это не то, что происходит.UITableView ReloadData confusion

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     //add code here for when you hit delete 

     NSDictionary *dict = [_sourceData valueForKey:[NSString stringWithFormat:@"%li", indexPath.row]]; 

     Connection *connection = [[Connection alloc] init]; 

     [connection removeRowFromTable:@"sources" withRowID:[dict valueForKey:@"id"]]; 

     [self loadData]; 

     NSLog(@"%@", _sourceData); 

     [_sourceTable reloadData]; 

    } 
} //Delete Data Cell 

Таким образом, он создает словарь с идентификатором строки. Затем инициализирует класс соединения, удаляет строку из таблицы (которая работает), загружает данные, где она заполняет _sourceData данными из таблицы. Запускает журнал _sourceData, который правильно регистрируется для удаленной. Однако перезагрузка данных для исходной таблицы не работает. Он удаляет строку a, но не правильный. то, если я должен выйти из этого диспетчера view и вернуться к нему, он отобразит правильные данные.

Например, если я удалю первую строку, она удалит вторую строку из представления в представлении таблицы, а затем, когда я вернусь к контроллеру представления. Теперь он удален правильно.

_sourceData регистрирует правильные данные, tableView не отображает его.

Я предоставлю все методы tableView, которые помогут.

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ 
    return 1; 
} 
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Return YES if you want the specified item to be editable. 
    return YES; 
} 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     //add code here for when you hit delete 

     NSDictionary *dict = [_sourceData valueForKey:[NSString stringWithFormat:@"%li", indexPath.row]]; 

     Connection *connection = [[Connection alloc] init]; 

     [connection removeRowFromTable:@"sources" withRowID:[dict valueForKey:@"id"]]; 

     [self loadData]; 

     NSLog(@"%@", _sourceData); 

     [_sourceTable reloadData]; 

    } 
} //Delete Data Cell 
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 
    [cell setBackgroundColor:_cellBackgroundColor]; 
} 
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return [_sourceData count]; 
} 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellIdentifier"]; 

     NSDictionary *innerDict = [_sourceData valueForKey:[NSString stringWithFormat:@"%i", (int)indexPath.row]]; 

     NSString *setText = [innerDict valueForKey:@"sourceName"]; 

     UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(_leftMargin, cell.frame.size.height/2 - 7.5, 150, 15)]; 
     label.textAlignment = NSTextAlignmentCenter; 
     [label setFont:[UIFont systemFontOfSize:_cellTitleFontSize]]; 
     [label setBackgroundColor:[UIColor clearColor]]; 
     [label setTextColor:_cellTextColor]; 
     [label setText:setText]; 
     [label sizeToFit]; 
     [cell addSubview:label]; 
    } 

    return cell; 
} //View Of cell 
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    return 50.0; 
} 

ответ

0

Хорошо, я, будучи идиотом, забыл, что я добавил метки в ручную, а не используя обычные методы.

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

    for (UIView *i in cell.subviews) { 
     if ([i isKindOfClass:[UILabel class]]) { 
      [i removeFromSuperview]; 
     } 
    } 
.... 

Удаляет предыдущие метки из этой ячейки перед добавлением новых данных.