select custom UITableViewCell by Identifier from xib
I try to fill my UITableView withd different UITableViewCells. I tried to choose 1 of 3 UITableViewCells out of a XIB-file by identifier but I can't get the value of the UITableViewCell's Identifier.
- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MyTableViewCell *cell = (MyTableViewCell *) [tableView dequeueReusableCellWithIdentifier:@"CellTwo"];
if(nil == cell){
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"MyTableViewCell" owner:self options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[MyTableViewCell class]]) {
MyTableViewCell *tmpCell = (MyTableViewCell *) currentObject;
if (tmpCell.identifier == @"CellTwo") {
cell = (MyTableViewCell *) currentObject;
}
[tmpCell release];
br开发者_StackOverflow中文版eak;
}
}
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;}
I solved this exact problem by creating a subclass for each UITableViewCell. When you do your comparison of the class type, you can put in the type of cell it is:
cell = (FooterTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"footerTableViewCell"];
if (cell == nil){
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CommonTableViewCell" owner:nil options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[FooterTableViewCell class]]){
cell = (FooterTableViewCell *)currentObject;
return cell;
}
}
}
I have some tables that reuse the exact same kind of cell formatting and i wanted to have them in a common XIB that i can alter at anytime. In the .h for that controller, i simply declared three sub classes of UITableViewCell for each of the three cell types.
精彩评论