GeneCode
GeneCode

Reputation: 7588

Correct way to populate TableView Cell

Since 2009, this is the way I populate tableView cells (custom cell):

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AccountTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AccountCellID"];
    if (cell==nil) {
        cell = [[AccountTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AccountCellID"];
    }

// here update content
    cell.customImg = [UIImage imageNamed:[NSString stringWithFormat:@"%ld.png",indexPath.row]]


    return cell;
}

Is this good or are there better ways? Some people said I should update everything in the TableViewcell custom class itself. Is there any difference?

Upvotes: 0

Views: 86

Answers (1)

Davinder Singh
Davinder Singh

Reputation: 645

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return Array.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FilterTableViewCell *cell = (FilterTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"filter cell"];
        BrandObjectClass *brandObject = [brandsArray objectAtIndex:indexPath.row];

        cell.lblFilterName.text = brandObject.brandName;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.lblFilterName.font = [UIFont fontWithName:@"Roboto-Medium" size:cell.lblFilterName.font.pointSize];
            cell.imgTick.hidden = NO;


        return cell;
}

When you are working with custom table view cell the casting is also important because dequeueReusableCellWithIdentifier returns object of UITableViewCell.

Hope this will help.

Upvotes: 1

Related Questions