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:NSStringそして2つの使い方を比較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

(self.pickerSelectedRow == [self.datePickerArray objectAtIndex:1]) が毎回満たされているかどうかは確かですか?

また、値 [self.datePickerArray objectAtIndex:1] を self.pickerSelectedRow 値と比較するのはなぜですか?

于 2013-05-16T11:15:30.050 に答える