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:
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"];
}
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];
}
替换isEqual:
by isEqualToString:
使用isEqualToString:
进行字符串比较
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?
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.