I am trying to change the custom cell that will be used depending on information stored in my array. I have tried everything but cannot seem to get it working without an error. The code does not seem to accept a string input for the cell nib. Is there any other way to do this? Will attach my code below.
Thanks.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if([[Tutortrackinbud[indexPath.row] valueForKey:@"reply"] isEqual: @"N"])
{
nib = @"ReviewCell1";
}
else{
nib = @"ReviewCell2";
}
static NSString *simpleTableIdentifier = nib;
ReviewCell1TableViewCell *cell = (ReviewCell1TableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:nib owner:self options:nil];
cell = [nibArray objectAtIndex:0];
}
if ([[Tutortrackinbud[indexPath.row] valueForKey:@"stars"] isEqual: @"1"]){
NSLog(@"1");
cell.star1.image = [UIImage imageNamed:@"star.png"];
cell.star2.image = [UIImage imageNamed:@"greystar.png"];
cell.star3.image = [UIImage imageNamed:@"greystar.png"];
cell.star4.image = [UIImage imageNamed:@"greystar.png"];
cell.star5.image = [UIImage imageNamed:@"greystar.png"];
cell.star6.image = [UIImage imageNamed:@"greystar.png"];
}
else if ([[Tutortrackinbud[indexPath.row] valueForKey:@"stars"] isEqual: @"2"]){
NSLog(@"2");
cell.star1.image = [UIImage imageNamed:@"star.png"];
cell.star2.image = [UIImage imageNamed:@"star.png"];
cell.star3.image = [UIImage imageNamed:@"greystar.png"];
cell.star4.image = [UIImage imageNamed:@"greystar.png"];
cell.star5.image = [UIImage imageNamed:@"greystar.png"];
cell.star6.image = [UIImage imageNamed:@"greystar.png"];
}
else if ([[Tutortrackinbud[indexPath.row] valueForKey:@"stars"] isEqual: @"3"]){
NSLog(@"3");
cell.star1.image = [UIImage imageNamed:@"star.png"];
cell.star2.image = [UIImage imageNamed:@"star.png"];
cell.star3.image = [UIImage imageNamed:@"star.png"];
cell.star4.image = [UIImage imageNamed:@"greystar.png"];
cell.star5.image = [UIImage imageNamed:@"greystar.png"];
cell.star6.image = [UIImage imageNamed:@"greystar.png"];
}
else if ([[Tutortrackinbud[indexPath.row] valueForKey:@"stars"] isEqual: @"4"]){
NSLog(@"4");
cell.star1.image = [UIImage imageNamed:@"star.png"];
cell.star2.image = [UIImage imageNamed:@"star.png"];
cell.star3.image = [UIImage imageNamed:@"star.png"];
cell.star4.image = [UIImage imageNamed:@"star.png"];
cell.star5.image = [UIImage imageNamed:@"greystar.png"];
cell.star6.image = [UIImage imageNamed:@"greystar.png"];
}
else if ([[Tutortrackinbud[indexPath.row] valueForKey:@"stars"] isEqual: @"5"]){
NSLog(@"5");
cell.star1.image = [UIImage imageNamed:@"star.png"];
cell.star2.image = [UIImage imageNamed:@"star.png"];
cell.star3.image = [UIImage imageNamed:@"star.png"];
cell.star4.image = [UIImage imageNamed:@"star.png"];
cell.star5.image = [UIImage imageNamed:@"star.png"];
cell.star6.image = [UIImage imageNamed:@"greystar.png"];
}
else if ([[Tutortrackinbud[indexPath.row] valueForKey:@"stars"] isEqual: @"6"]){
NSLog(@"6");
cell.star1.image = [UIImage imageNamed:@"star.png"];
cell.star2.image = [UIImage imageNamed:@"star.png"];
cell.star3.image = [UIImage imageNamed:@"star.png"];
cell.star4.image = [UIImage imageNamed:@"star.png"];
cell.star5.image = [UIImage imageNamed:@"star.png"];
cell.star6.image = [UIImage imageNamed:@"star.png"];
}
cell.reviewdate.text = [Tutortrackinbud[indexPath.row] valueForKey:@"date"];
cell.reviewtext.text = [NSMutableString stringWithFormat:@"%@ -%@",[Tutortrackinbud[indexPath.row] valueForKey:@"desc"],[Tutortrackinbud[indexPath.row] valueForKey:@"username"]];
//cell.tutortext.text = [NSMutableString stringWithFormat:@"%@ -%@",[Tutortrackinbud[indexPath.row] valueForKey:@"desc"],[Tutortrackinbud[indexPath.row] valueForKey:@"username"]];
cell.tutortext.text = @"this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test this is test";
return cell;
}
You have to register your nib file preferably in your viewDidLoad
Sample code :
UINib *cellNib = [UINib nibWithNibName:@"MyCustomCell" bundle:nil];
[self.tableView registerNib:self.cellNib forCellReuseIdentifier:@"CustomCell"];
and then use dequeueReusableCellWithIdentifier
normally as you have done already. you can register multiple nib
files with different identifiers and just dequeue them in cellForRowAtIndexPath
with their unique identifiers.
In my projects I often do a base cell with class methods like this:
+ (NSString *)reuseIdentifier {
return NSStringFromClass([self class]);
}
+ (UINib *)nibName {
return [UINib nibWithNibName:NSStringFromClass([self class]) bundle:nil];
}
And all other cells subclass from it and use it this way where it needed:
[self.tableView registerNib:[UINib nibWithNibName:[FeedCell nibName] bundle:nil] forCellReuseIdentifier:[FeedCell reuseIdentifier]];
Hope it helps)
I suggest , Rather then multiple xib
file you need to create single xib
file and you get the object from NSArray
.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier = nib;
ReviewCell1TableViewCell *cell = (ReviewCell1TableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil){
NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:nib owner:self options:nil];
if([[Tutortrackinbud[indexPath.row] valueForKey:@"reply"] isEqual: @"N"]){
cell = [nibArray objectAtIndex:0];
}else{
cell = [nibArray objectAtIndex:1];
}
}
return cell;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.