2012-12-21 46 views
0

我有一个设置好的tableView。 正如您在下面看到的,当对象@“发送”的值更改为@“是”时,我想将图像添加到我的单元格中。完成后我调用[sendTable reloadData],但tableview没有任何反应。只有当我重新启动应用程序时,我的图像才能正确显示在单元格中。 所以显然reloadData在这里没有做任何事情。更改数据后重新加载自定义单元格

有人可以找出问题吗?

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

UITableViewCell *cell; 

if (tableView == sendTable) { 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"savedItems.plist"]; 
    savedItemsArray = [NSMutableArray arrayWithContentsOfFile:writableDBPath]; 

    NSDictionary* item = [savedItemsArray objectAtIndex:indexPath.row]; 

    NSString *temp = [NSString stringWithFormat:@"%@,%@", [[savedItemsArray valueForKey:@"name"]objectAtIndex:indexPath.row], 
         [[savedItemsArray valueForKey:@"message"]objectAtIndex:indexPath.row]]; 

    UIView *background; 

    cell = [self.sendTable dequeueReusableCellWithIdentifier:temp]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:temp]; 
     cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 
     cell.selectionStyle = UITableViewCellSelectionStyleGray; 

     background = [[UIView alloc] initWithFrame:cell.frame]; 

     [cell addSubview:background]; 
     [cell sendSubviewToBack:background]; 

    } 

    if ([[item objectForKey:@"sent"] isEqualToString: @"no"]) { 

     UILabel *personLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, cell.frame.size.width-150, 20)]; 
     personLabel.backgroundColor = [UIColor clearColor]; 
     personLabel.text = [NSString stringWithFormat: @"%@",[item objectForKey:@"name"]]; 
     personLabel.font = [UIFont boldSystemFontOfSize:17]; 

     UILabel *personLabel2 = [[UILabel alloc]initWithFrame:CGRectMake(10, 30, cell.frame.size.width-150, 20)]; 
     personLabel2.backgroundColor = [UIColor clearColor]; 
     personLabel2.text = [NSString stringWithFormat: @"(%@)", [item objectForKey:@"number"]]; 
     personLabel2.font = [UIFont systemFontOfSize:15]; 

     UILabel *dateLabel = [[UILabel alloc]initWithFrame:CGRectMake(cell.frame.size.width-125, 10, 80, 40)]; 
     dateLabel.backgroundColor = [UIColor clearColor]; 
     dateLabel.text = [NSString stringWithFormat: @"%@\n%@",[item objectForKey:@"date"], [item objectForKey:@"time"]]; 
     dateLabel.textColor = [UIColor darkGrayColor]; 
     dateLabel.textAlignment = NSTextAlignmentRight; 
     dateLabel.font = [UIFont systemFontOfSize:13]; 
     dateLabel.numberOfLines = 2; 

     UILabel *messageLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 60, cell.frame.size.width-50, 20)]; 
     messageLabel.backgroundColor = [UIColor clearColor]; 
     messageLabel.text = [NSString stringWithFormat: @"%@",[item objectForKey:@"message"]]; 
     messageLabel.textColor = [UIColor lightGrayColor]; 
     messageLabel.font = [UIFont systemFontOfSize:15]; 

     [background addSubview:personLabel]; 
     [background addSubview:personLabel2]; 
     [background addSubview:dateLabel]; 
     [background addSubview:messageLabel]; 
     [cell addSubview:background]; 

    }else{ 

     UILabel *personLabel = [[UILabel alloc]initWithFrame:CGRectMake(40, 10, cell.frame.size.width-170, 20)]; 
     personLabel.backgroundColor = [UIColor clearColor]; 
     personLabel.text = [NSString stringWithFormat: @"%@",[item objectForKey:@"name"]]; 
     personLabel.font = [UIFont boldSystemFontOfSize:17]; 

     UILabel *personLabel2 = [[UILabel alloc]initWithFrame:CGRectMake(40, 30, cell.frame.size.width-170, 20)]; 
     personLabel2.backgroundColor = [UIColor clearColor]; 
     personLabel2.text = [NSString stringWithFormat: @"(%@)", [item objectForKey:@"number"]]; 
     personLabel2.font = [UIFont systemFontOfSize:15]; 

     UILabel *dateLabel = [[UILabel alloc]initWithFrame:CGRectMake(cell.frame.size.width-125, 10, 80, 40)]; 
     dateLabel.backgroundColor = [UIColor clearColor]; 
     dateLabel.text = [NSString stringWithFormat: @"%@\n%@",[item objectForKey:@"date"], [item objectForKey:@"time"]]; 
     dateLabel.textColor = [UIColor darkGrayColor]; 
     dateLabel.textAlignment = NSTextAlignmentRight; 
     dateLabel.font = [UIFont systemFontOfSize:13]; 
     dateLabel.numberOfLines = 2; 

     UILabel *messageLabel = [[UILabel alloc]initWithFrame:CGRectMake(40, 60, cell.frame.size.width-80, 20)]; 
     messageLabel.backgroundColor = [UIColor clearColor]; 
     messageLabel.text = [NSString stringWithFormat: @"%@",[item objectForKey:@"message"]]; 
     messageLabel.textColor = [UIColor lightGrayColor]; 
     messageLabel.font = [UIFont systemFontOfSize:15]; 

     UIImage *checkmarkImage = [UIImage imageNamed:@"done.png"]; 
     UIImageView *checkmark = [[UIImageView alloc] initWithImage:checkmarkImage]; 
     [checkmark setFrame:CGRectMake(10, 35, 20, 20)]; 

     [background addSubview:personLabel]; 
     [background addSubview:personLabel2]; 
     [background addSubview:dateLabel]; 
     [background addSubview:messageLabel]; 
     [background addSubview:checkmark]; 
     [cell addSubview:background]; 

    } 

} 
return cell; 
} 

非常感谢!

回答

0

当你添加几个UILabel和UIView到一个单元格时,我想你应该定制你自己的UITableViewCell。 尝试创建一个类继承UITableviewCell,并在实现中添加子视图。 希望我的回答有帮助

相关问题