更改数据后重新加载自定义单元格出错

时间:2012-12-21 21:28:48

标签: iphone xcode uitableview sdk cell

我有一个设置tableView,工作正常。 正如您在下面看到的,当对象@“sent”的值更改为@“yes”时,我想向我的单元格添加一个图像。完成此操作后,我调用[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;
}

非常感谢!

1 个答案:

答案 0 :(得分:0)

当您向单元格添加多个UILabel和UIView时,我认为您应该自定义您自己的UITableViewCell。 尝试创建一个子类化UITableviewCell的类,并在工具中添加子视图。 希望我的回答很有帮助