chevron-up bell reply instagram twitter2 feed3 finder search-25px-p0

iOS TableView Development Tips

2017-05-05

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

Change the tableview background color: Sometimes you probably need to change the whole table view background.

self.tableView.backgroundColor = colorTableview;

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

Remove blank table rows: If you don't do this, there will be extra empty table cells in your tableview. See below image:

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

TableView overlapped with Navigation Bar:

self.navigationController.navigationBar.translucent = NO;

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;
    }
}


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; 
}

Make your TableView Cell Non-Selectable/Non-highlightable:

There are two ways of doing this:

Method 1: Use Storyboard:

Method 2: Use Code:

Swift

cell.selectionStyle = UITableViewCellSelectionStyle.none

Objective-C

cell.selectionStyle = UITableViewCellAccessoryNone;

Reference:
http://www.arkilis.me/?p=408

arkilis

Make Your Comments