Reputation: 2512
I have tableView and i want to set the different image for every cell in my tableView based of certain condition but i am getting the image of last satisfied condition for all cells.
here is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
cell.textLabel.numberOfLines = 2;
if (self.pickerSelectedRow == [self.datePickerArray objectAtIndex:1])
{
for (int i=0; i<[appDelegate.serverResponseArray count]; i++)
{
if([[[appDelegate.serverResponseArray objectAtIndex:i]valueForKey:@"session_type" ] isEqual: @"workshop"])
{
cell.imageView.image = [UIImage imageNamed:@"workshops.png"];
}
if([[[appDelegate.serverResponseArray objectAtIndex:i]valueForKey:@"session_type" ] isEqual: @"keynote"])
{
cell.imageView.image = [UIImage imageNamed:@"keynote.png"];
}
if([[[appDelegate.serverResponseArray objectAtIndex:i]valueForKey:@"session_type" ] isEqual: @"panel"]|| [[[appDelegate.serverResponseArray objectAtIndex:i]valueForKey:@"session_topic" ] isEqual: @"WISNET"])
{
cell.imageView.image = [UIImage imageNamed:@"pannel.png"];
}
if([[[appDelegate.serverResponseArray objectAtIndex:i]valueForKey:@"session_type" ] isEqual: @"social"] || [[[appDelegate.serverResponseArray objectAtIndex:i]valueForKey:@"session_topic" ] isEqual: @"RWW"])
{
cell.imageView.image = [UIImage imageNamed:@"social.png"];
}
}
cell.textLabel.text = [[self.globalSessionArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
}
Expected output:
output i am getting:
Upvotes: 1
Views: 2747
Reputation: 3658
Fill your UITableViewCell
content in tableView:willDisplayCell:forRowAtIndexPath:
method. And to compare 2 NSString
use isEqualToString:
method.
EDIT:
- (void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath
{
cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
cell.textLabel.numberOfLines = 2;
if (self.pickerSelectedRow == [self.datePickerArray objectAtIndex:1])
{
for (int i=0; i<[appDelegate.serverResponseArray count]; i++)
{
NSString* session_type = [[appDelegate.serverResponseArray objectAtIndex:i]valueForKey:@"session_type"];
if([seesion_type isEqualToString: @"workshop"])
cell.imageView.image = [UIImage imageNamed:@"workshops.png"];
else if([session_type isEqualToString: @"keynote"])
cell.imageView.image = [UIImage imageNamed:@"keynote.png"];
else if([session_type isEqualToString: @"panel"]|| [[[appDelegate.serverResponseArray objectAtIndex:i]valueForKey:@"session_topic" ] isEqualToString: @"WISNET"])
cell.imageView.image = [UIImage imageNamed:@"pannel.png"];
else if([session_type isEqualToString: @"social"] || [[[appDelegate.serverResponseArray objectAtIndex:i]valueForKey:@"session_topic" ] isEqual: @"RWW"])
cell.imageView.image = [UIImage imageNamed:@"social.png"];
}
cell.textLabel.text = [[self.globalSessionArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
}
Upvotes: 1
Reputation: 1049
Are you sure if (self.pickerSelectedRow == [self.datePickerArray objectAtIndex:1]) is satisfied every time?
Also why are you comparing value [self.datePickerArray objectAtIndex:1] with self.pickerSelectedRow value?
Upvotes: 0
Reputation: 2451
in your code it will always return i = 0;
maybe you can try something like:
if([[[appDelegate.serverResponseArray objectAtIndex:indexPath.row]valueForKey:@"session_type" ] isEqualToString: @"workshop"]) { cell.imageView.image = [UIImage imageNamed:@"workshops.png"]; } if([[[appDelegate.serverResponseArray objectAtIndex:indexPath.row]valueForKey:@"session_type" ] isEqualToString: @"keynote"]) { cell.imageView.image = [UIImage imageNamed:@"keynote.png"]; } if([[[appDelegate.serverResponseArray objectAtIndex:indexPath.row]valueForKey:@"session_type" ] isEqualToString: @"panel"]|| [[[appDelegate.serverResponseArray objectAtIndex:indexPath.row]valueForKey:@"session_topic" ] isEqualToString: @"WISNET"]) { cell.imageView.image = [UIImage imageNamed:@"pannel.png"]; } if([[[appDelegate.serverResponseArray objectAtIndex:indexPath.row]valueForKey:@"session_type" ] isEqualToString: @"social"] || [[[appDelegate.serverResponseArray objectAtIndex:indexPath.row]valueForKey:@"session_topic" ] isEqualToString: @"RWW"]) { cell.imageView.image = [UIImage imageNamed:@"social.png"]; }
Upvotes: 1