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

Iphone 如何为uiTableView中的每个单元格设置不同的图像。?,iphone,ios,objective-c,uitableview,uiimageview,Iphone,Ios,Objective C,Uitableview,Uiimageview,我有tableView,我想根据特定条件为tableView中的每个单元格设置不同的图像,但我得到的是所有单元格最后满足条件的图像 这是我的密码: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cel

我有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];
    } 
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*CellIdentifier=@“Cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle重用标识符:CellIdentifier];
}
cell.textlab.font=[UIFont boldSystemFontOfSize:14];
cell.textlab.numberOfLines=2;
if(self.pickerSelectedRow==[self.datePickerArray objectAtIndex:1])
{

对于(int i=0;i使用
isEqualToString:
进行字符串比较

在代码中,它将始终返回
i=0

也许你可以尝试以下方式:

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"]; }
if([[[appDelegate.serverResponseArray objectAtIndex:indexPath.row]valueForKey:@“会话类型”]IsequalString:@“车间”]) { cell.imageView.image=[UIImage ImageName:@“workshops.png”]; } if([[[appDelegate.serverResponseArray objectAtIndex:indexPath.row]valueForKey:@“会话类型”]IsequalString:@“注释记号”]) { cell.imageView.image=[UIImage ImageName:@“keynote.png”]; } 如果([[[appDelegate.serverResponseArray objectAtIndex:indexPath.row]valueForKey:@“会话类型”]IseQualString:@“面板”].[appDelegate.serverResponseArray objectAtIndex:indexPath.row]valueForKey:@“会话主题”]IseQualString:@“WISNET”]) { cell.imageView.image=[UIImage ImageName:@“pannel.png”]; } if([[[appDelegate.serverResponseArray objectAtIndex:indexPath.row]valueForKey:@“会话类型”]IsequalString:@“社交”]|[appDelegate.serverResponseArray objectAtIndex:indexPath.row]valueForKey:@“会话主题”]IsequalString:@“RWW”]) { cell.imageView.image=[UIImage ImageName:@“social.png”]; }
表格视图:willDisplayCell:forRowAtIndexPath:
方法中填写
UITableViewCell
内容。要比较2
NSString
请使用
IsequalString:
方法

编辑:

-(void)tableView:(UITableView*)tableView将显示单元格:(UITableViewCell*)用于rowatindexpath的单元格:(nsindepath*)indepath
{
cell.textlab.font=[UIFont boldSystemFontOfSize:14];
cell.textlab.numberOfLines=2;
if(self.pickerSelectedRow==[self.datePickerArray objectAtIndex:1])
{

对于(int i=0;i替换
isEqual:
IseQualString:

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


另外,为什么要比较值[self.datePickerArray objectAtIndex:1]使用self.pickerSelectedRow值?

从cellForRowAtIndexPath方法和用户indexPath.row中删除for循环以获取索引。我建议您做两件事:首先使用
else if
lader,如果有可能一次执行一个条件。然后通过设置断点检查是否满足条件。通过使用此选项,我获得了第一个条件的输出。更新了我的代码,也许你可以再试一次。将isEqual更改为IsequalToString重试。这也不会产生任何效果。仍然是相同的输出。也许你应该检查条件以验证结果。尝试将断点设置为I。这会产生任何效果吗?是的。输出即将出现对于第一个满足的条件。不是我所期望的。我认为你应该逐行调试似乎还可以,你能告诉我它是哪一个条件并且总是显示橙色吗?好的。我现在逐行调试,然后我会在这里回复。在逻辑和条件上做了一些小的更改,最后得到了使用该ta的预期输出bleView方法。任何示例都将不胜感激。
- (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];
}