iOS TableView Development Tips

The following are some frequently used iOS TableView tips while developing.

1. Change the tableview background color

Sometimes you probably need to change the whole table view background

self.tableView.backgroundColor = colorTableview;

2. Reload the data in Table view

After update the table data array/dictionary, you probably want to manually reload the whole table.

[self.fileSelectionTableView reloadData];

3. Remove blank table rows

By default there will be extra empty table cells in your tableview. See below image:

To remove all the empty rows:

// remove all empty rows
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

4. TableView overlapped with Navigation Bar

It is a quite common issue if you have placed a tableView on the UIViewController, or use code to draw the tableView.

self.navigationController.navigationBar.translucent = NO;

5. Handle Even and Odd TableCell

Sometimes you want to treat differently on even and odd table cells.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row %2 == 0){
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"dopboxCell" forIndexPath:indexPath];
        // Configure the cell...
        NSString *sectionTitle = [itemTitles objectAtIndex:indexPath.section];
        NSArray *sectionItems = [items objectForKey:sectionTitle];
        NSString *item = [sectionItems objectAtIndex:indexPath.row];
        cell.textLabel.text = item;
        //cell.imageView.image = [UIImage imageNamed:[self getImageFilename:animal]];
        return cell;
    }
    else{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"icloudCell" forIndexPath:indexPath];
        // Configure the cell...
        NSString *sectionTitle = [itemTitles objectAtIndex:indexPath.section];
        NSArray *sectionItems = [items objectForKey:sectionTitle];
        NSString *item = [sectionItems objectAtIndex:indexPath.row];
        cell.textLabel.text = item;
        //cell.imageView.image = [UIImage imageNamed:[self getImageFilename:animal]];
        return cell;
    }
    else{
        return nil;
    }
}

6. Pull down to refresh the table

//Step 1: in the viewDidLoad method, 

// load data items at the start
[self wrapperDownLoadedItems];

self.refreshControl = [[UIRefreshControlalloc] init];
self.refreshControl.backgroundColor = [UIColor purpleColor];
self.refreshControl.tintColor = [UIColor whiteColor];
[self.refreshControl addTarget:self action:@selector(wrapperDownLoadedItems) forControlEvents:UIControlEventValueChanged];

//Step 2: implement the method wrapperDownLoadedItems

-(void) wrapperDownLoadedItems{

    // _feedItems is table data source array
    _feedItems = [[NSArray alloc] initObjects:XXXXXX];
    // need to reload it 
    [self.listTableView reloadData];

    // populate the table view


 self.listTableView.delegate = self;
    self.listTableView.dataSource = self; 
}

7. Make your TableView Cell Non-Selectable/Non-highlightable

There are two ways of doing so:

Method 1: Use Storyboard:

Method 2: Use Code:

Swift

cell.selectionStyle = UITableViewCellSelectionStyle.none

Objective-C

cell.selectionStyle = UITableViewCellAccessoryNone;
comments powered by Disqus