我正在制作一个inboxmodule,其中我正在使用表视图,我的tableview数据来自服务器我坚持使用它我想根据来自服务器的标志更改tablecell颜色if messagereadflag = 0然后我想要单元格的灰色,如果消息读取标志= 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提前帮助:(
答案 0 :(得分:2)
使用 cell.contentView.backgroundColor 代替 cell.backgroundcolor 属性。
在 cellForRowAtIndexPath 方法
中尝试使用此代码if([[self.inboxmessagesarray objectAtIndex:indexPath.row]objectForKey:@"messageRead"] intValue]==0)
{
cell.contentView.backgroundColor=[UIColor grayColor];
}
else
{
cell.contentView.backgroundColor=[UIColor clearColor];
}
或使用 willDisplayCell 方法
- (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];
}
}
答案 1 :(得分:2)
使用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];
}
}
答案 2 :(得分:1)
在
中添加条件-(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];
}