如何在uiTableView中为每个单元格设置不同的图像。

时间:2013-05-16 10:23:37

标签: iphone ios objective-c uitableview uiimageview

我有tableView,我想根据特定条件为tableView中的每个单元格设置不同的图像,但我得到的是所有单元格的最后满足条件的图像。

这是我的代码:

- (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];
    } 

预期产量: i am expecting output something like this (ignore the content of cells.)

我得到的输出:

i am getting output something like this (ignore the content of cells.)

5 个答案:

答案 0 :(得分:1)

在您的代码中,它将始终返回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"];
        }

答案 1 :(得分:1)

UITableViewCell方式填写tableView:willDisplayCell:forRowAtIndexPath:内容。并比较2 NSString使用isEqualToString:方法。

编辑:

- (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];
}

答案 2 :(得分:1)

isEqual:替换为isEqualToString:

答案 3 :(得分:0)

使用isEqualToString:进行字符串比较

答案 4 :(得分:0)

您是否确定每次都满足(self.pickerSelectedRow == [self.datePickerArray objectAtIndex:1])?

另外,为什么要将value [self.datePickerArray objectAtIndex:1]与self.pickerSelectedRow值进行比较?