Ios 如何根据来自服务器的标志更改uitableviewcell的颜色

Ios 如何根据来自服务器的标志更改uitableviewcell的颜色,ios,uitableview,Ios,Uitableview,我正在制作一个inboxmodule,其中我使用的是table view,我的tableview数据来自服务器。我被困于此,我想根据来自服务器的标志更改tablecell颜色。如果messagereadflag=0,则我希望单元格颜色为灰色,如果message read flag=1,则我希望颜色清晰,以便用户能够轻松区分在已读和未读消息之间。我的回答是,当标志从下面的服务器传来时,如何做到这一点 -(void)inboxmessagesGetSuccess:(FBGenericWebHandl

我正在制作一个inboxmodule,其中我使用的是table view,我的tableview数据来自服务器。我被困于此,我想根据来自服务器的标志更改tablecell颜色。如果messagereadflag=0,则我希望单元格颜色为灰色,如果message read flag=1,则我希望颜色清晰,以便用户能够轻松区分在已读和未读消息之间。我的回答是,当标志从下面的服务器传来时,如何做到这一点

-(void)inboxmessagesGetSuccess:(FBGenericWebHandler*)handler response:(NSDictionary*)response
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];

    NSMutableArray *inboxArray = [[NSMutableArray alloc] init];
    NSArray *firstarray=[[[response objectForKey:@"messageResponse"] objectAtIndex:1] objectForKey:@"messages"];
    for(NSDictionary *tmp in firstarray)
    {
        NSMutableDictionary *messages=[[NSMutableDictionary alloc]init];
        [messages setValue:[tmp objectForKey:@"messageId"] forKey:@"messageId"];
        [messages setValue:[tmp objectForKey:@"messageDate"] forKey:@"messageDate"];
        [messages setValue:[tmp objectForKey:@"messageTime"] forKey:@"messageTime"];
        [messages setValue:[tmp objectForKey:@"offerTitle"] forKey:@"offerTitle"];
        [messages setValue:[tmp objectForKey:@"messageRead"] forKey:@"messageRead"];// here are coming flags
        [self.inboxmessagesarray addObject:messages];
    }
    [self.tb reloadData];
    }
下面是我的tableview单元格代码:

  -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *tableviewidentifier = @"cell";
    tablecellTableViewCell *cell= [self.activitiesTableView_ dequeueReusableCellWithIdentifier:tableviewidentifier];

    if(cell==nil)
    {
        cell = [[tablecellTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:tableviewidentifier];
    }if(indexPath.row == [self tableView:tableView numberOfRowsInSection:indexPath.section] - 1){

    }

    UILabel *valuedate = (UILabel *)[cell viewWithTag:21];
    UILabel *msg = (UILabel *)[cell viewWithTag:22];
    UILabel *date = (UILabel *)[cell viewWithTag:23];
    UILabel *time = (UILabel *)[cell viewWithTag:24];
    if([[[self.inboxmessagesarray objectAtIndex:indexPath.row]objectForKey:@"messageRead"] intValue]==0)
    {
        cell.backgroundColor=[UIColor grayColor];
    }
    else
    {
        cell.backgroundColor=[UIColor clearColor];
    }
    valuedate.text=[[self.inboxmessagesarray objectAtIndex:indexPath.row]objectForKey:@"offerTitle"];
    msg.text=@"How are you?";
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
        NSString *img=[[self.inboxmessagesarray objectAtIndex:indexPath.row]objectForKey:@"offerPhoto"];// here i am getting image path
        NSURL *url = [NSURL URLWithString:img];
        NSData * imageData = [NSData dataWithContentsOfURL:url];
        UIImage *image = [UIImage imageWithData:imageData];

        dispatch_sync(dispatch_get_main_queue(), ^{ //in main thread update the image
            cell.imageView.image = image;
            cell.textLabel.text = @""; //add this update will reflect the changes
        });
    });



    date.text=[[self.inboxmessagesarray objectAtIndex:indexPath.row]objectForKey:@"messageDate"];
    time.text=[[self.inboxmessagesarray objectAtIndex:indexPath.row]objectForKey:@"messageTime"];


    return cell;
    }
thanx提前请帮助:

在中添加条件

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
它检查标志值

    if([[self.inboxmessagesarray objectAtIndex:indexPath.row]objectForKey:@"messageRead"] intValue]==0)
    {
        cell.backgroundColor=[UIColor grayColor];
    }
    else
    {
        cell.backgroundColor=[UIColor clearColor];
    }
添加条件

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
它检查标志值

    if([[self.inboxmessagesarray objectAtIndex:indexPath.row]objectForKey:@"messageRead"] intValue]==0)
    {
        cell.backgroundColor=[UIColor grayColor];
    }
    else
    {
        cell.backgroundColor=[UIColor clearColor];
    }
使用cell.contentView.backgroundColor而不是cell.backgroundColor属性

请尝试在CellForRowatineXpath方法中使用此代码

或者使用willDisplayCell方法

使用cell.contentView.backgroundColor而不是cell.backgroundColor属性

请尝试在CellForRowatineXpath方法中使用此代码

或者使用willDisplayCell方法


使用UITableView委托方法更新单元格

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

if([[self.inboxmessagesarray objectAtIndex:indexPath.row]objectForKey:@"messageRead"] intValue]==0)
    {
        cell.backgroundColor=[UIColor grayColor];
    }
    else
    {
        cell.backgroundColor=[UIColor clearColor];
    }

}

使用UITableView委托方法更新单元格

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

if([[self.inboxmessagesarray objectAtIndex:indexPath.row]objectForKey:@"messageRead"] intValue]==0)
    {
        cell.backgroundColor=[UIColor grayColor];
    }
    else
    {
        cell.backgroundColor=[UIColor clearColor];
    }

}


制作一个标签字典作为键,颜色对象作为值,然后在从服务器获取标签后,从字典中获取颜色对象作为标签键。我不明白。你能提供任何示例吗?你就是我在复选框计数之前回答的那个人吗?是的,我正在处理我在问题中提到的同一个项目标签字典作为键,颜色对象作为值,然后从服务器获取标签后,从字典中获取颜色对象作为标签键。我不明白。您能提供任何示例吗?在复选框计数之前,您就是我回答的那个人吗?是的,我正在处理问题中提到的同一项目UITableViewCell*tableView:UITableView*tableView cellForRowAtIndexPath:NSIndexPath*indexPath{在返回单元格之前;k等待,我会让你告诉我添加了,但是没有发生任何事情。你在这个单元格中有很多标签。因此,它们必须是清晰的彩色背景来检查这一点,就像它们有背景色一样,你看不到单元格的背景色。我已经用你给定的代码更新了我的问题。请查看UITableViewCell*tableView:UITableView*tableView单元格for行索引路径:nsindepath*indepath{在返回单元格之前;k等待,我会让你告诉我添加了什么,但是没有发生任何事情。你在这个单元格中有很多标签。因此,它们必须是清晰的颜色背景来检查这一点,就像它们有背景色一样,你看不到单元格的背景色。我已经用你给定的代码更新了我的问题,请查看一下。我还想查一下在btn单击时也要更改颜色,但在btn单击时我也在设置内容视图,但灰色不显示?我也想在btn单击时更改颜色,但在btn单击时我也在设置内容视图,但灰色不显示?