I’m trying to make a subclass of UITableViewCell, which simply contains a UILabel Property “nameLabel” and a UILabel Property “statusLabel”.To dequeue the cells correctly, I implemented the cellForRowAtIndexPath: method like this:
PS: The text of statusLabel is from twitter API, so I have to send the size of it to [[UILabel alloc]initWithFrame:]
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *CellIdentifier = @"StatusCellView";
StatusCellView *cell = [tableView dequeueReusableCellWithIdentifier:
CellIdentifier];
if (cell == nil) {
cell = [[StatusCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
for (UIView *subView in cell.contentView.subviews) {
[subView removeFromSuperview];
}
if (![cell viewWithTag:kNameLabelTag]) {
//init the nameLabel and [cell.contentView addsubview:nameLabel]
}
if (![cell viewWithTag:kStatusLabelTag]) {
//init the statusLabel and [cell.contentView addsubview:statusLabel]
}
return cell;
As you can see, this works well when the data is not so large. But my question is :
- I think removing and adding the subview repeatedly will make the performance bad. It’s a rude way…
- I can implement the same function without subclassing UITableViewCell. Is there any way to make use of the subclass?
I don’t really understand why you have to remove them…this seems like a perfectly reasonable way of doing it: