0

我有一个设置好的 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;
}

非常感谢!

4

1 回答 1

0

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

于 2012-12-23T05:22:57.767 回答