问题
My goal is to display a list of cells that are populated by some data I pull down from a server. And as the user scrolls down I would like there to be, briefly, a table cell that says "Loading More", and then goes away as more cells are filled in with data.
Here are the relevant sections to do this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView registerClass:[CGSitesCell class] forCellReuseIdentifier:cellIdentifier];
// Option 1: CGSitesCell *cell = [[CGSitesCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
// Option 2: CGSitesCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
//(Still Option 2):
//if(cell == nil){
// cell = [[CGSitesCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
if(backupsArray.count !=0){
//if we not on the last row
if (indexPath.row < backupsArray.count) {
[cell.textLabel setText:someData];
}else{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIdentifier];
[cell.textLabel setText:@"Load More"];
return cell;
}
return cell;
}
Here is what has me stumped: Option 1 works. I see the data I want to see and the Load More at the bottom. However, Option 2 does not work! I see the data, but I only see a BLANK table cell. Why?
Thanks!
回答1:
I came cross you question and dont know if you found the problem, but if you custom cell is built with Interface Builder then you should register the Nib, not the class:
[self.tableView registerNib:[UINib nibWithNibName:@"CGSitesCell" bundle:nil] forCellReuseIdentifier:@"CGSitesCellIdentifier"];
And please do it in ViewDidLoad, not in cellForRowAtIndexPath which is called for all your cells :-)
回答2:
I've done this by making the "Load More" as a UIView
and setting it as the UITableView
footer view instead of making it a table cell and then only making it visible when the last row of the table is visible using
`- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row == [backupsArray count] - 1) {
//load more records here
}
}`
I also had to set a BOOL
instance variable to keep track of when I was loading more because this method gets called multiple times the last cell is visible and I don't want to load more records repeatedly.
来源:https://stackoverflow.com/questions/17396042/dequeuereusablecellwithidentifier-causes-blank-table-cell