Search code examples
iosuitableviewcell

UITableView use xib to set cell,when Sliding it not smooth?


I have use xib set the interface,when using it dosen't smooth,why?

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

    if (!cell) {
        NSArray * nib = [[NSBundle mainBundle] loadNibNamed:@"MSNeedCheckCell" owner:self options:nil] ;
        cell = [nib objectAtIndex:0];
    }
//填充cell的内容
}

Solution

  • Try calling the following method from your init method for the class:

    [self.tableView registerNib:[UINib nibWithNibName:@"MSNeedCheckCell" bundle:nil]
         forCellReuseIdentifier:@"CustomerCell"];
    

    And then, you can remove your entire if (!cell) { section of code since it will always return a cell. This only loads the nib once instead of every time that you need a cell so will be much faster.