Search code examples
iphoneuitableviewnsindexpath

How to reload a single section in a UITableView


Hey I'm having trouble trying to figure this one out. UITableView has a method [tableView reloadSections:(NSIndexSet *) withRowAnimation:(UITableViewRowAnimation)] now according to the Apple docs this method takes in an NSIndexSet object and reloads what ever section specified by the index set. The problem is even though im only sending in one section to this method it ends up reloading all the sections and i can't understand why this is happening. Any help will be greatly appreciated


Solution

  • Even if you try reloading only a section, you must also provide the populating process section by section:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"DetailViewCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:CellIdentifier] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
       if (indexPath.section == 0){
          //blablabla
          cell.textLabel = blabla
       }
       else if (indexPath.section == 1){
          ///blablabla
          cell.textLabel = blabla2
       }
    return cell;
    }
    

    ... because reloading the table (is it only a section or the whole table) wil call the method above