1

我有 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];
    } 

预期输出: 我期待这样的输出(忽略单元格的内容。)

我得到的输出:

我得到这样的输出(忽略单元格的内容。)

4

5 回答 5

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"];
        }
于 2013-05-16T10:30:26.427 回答
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];
}
于 2013-05-16T10:56:12.053 回答
1

替换isEqual:isEqualToString:

于 2013-05-16T10:56:16.167 回答
0

用于isEqualToString:字符串比较

于 2013-05-16T10:27:24.693 回答
0

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

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

于 2013-05-16T11:15:30.050 回答