UITableViewCell不显示

时间:2014-11-14 08:53:00

标签: ios objective-c xcode uitableview

我的TableViewController有问题。有一个自定义单元格,一个类,以及动态加载的各种信息。我的TableViewController出现了,但我的单元格没有显示,但是我可以触摸它,我的信息过渡很好。 谢谢你的回答。

TableViewController.m

@interface Chat() {
    NSMutableArray *messages;
    UIRefreshControl *refreshControl;
}

@property (strong, nonatomic) IBOutlet UITableView *tableMessages;

@end

@implementation Chat

NSString *cellIdentifier = @"ChatCell";

- (void)viewDidLoad {
    [super viewDidLoad];


    [_tableMessages registerClass:[ChatCell class] forCellReuseIdentifier:cellIdentifier];

    refreshControl = [[UIRefreshControl alloc] init];
    [refreshControl addTarget:self action:@selector(loadMessages) forControlEvents:UIControlEventValueChanged];
    [_tableMessages addSubview:refreshControl];

    messages = [[NSMutableArray alloc] init];

    [self loadMessages];
}

- (void)loadMessages {

    if ([PFUser currentUser] != nil)
    {
        PFQuery *query = [PFQuery queryWithClassName:PF_MESSAGES_CLASS_NAME];
        [query whereKey:PF_MESSAGES_USER equalTo:[PFUser currentUser]];
        [query includeKey:PF_MESSAGES_LASTUSER];
        [query orderByDescending:PF_MESSAGES_UPDATEDACTION];
        [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
             if (error == nil) {
                 [messages removeAllObjects];
                 [messages addObjectsFromArray:objects];
                 [_tableMessages reloadData];
             } else [ProgressHUD showError:@"Network error."];
             [refreshControl endRefreshing];
         }];
    }
}

- (void)actionCleanup {
    [messages removeAllObjects];
    [_tableMessages reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [messages count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ChatCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    [cell bindData:messages[indexPath.row]];

    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    DeleteMessageItem(messages[indexPath.row]);
    [messages removeObjectAtIndex:indexPath.row];
    [_tableMessages deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    PFObject *message = messages[indexPath.row];
    ChatView *chatView = [[ChatView alloc] initWith:message[PF_MESSAGES_ROOMID]];
    [self.navigationController pushViewController:chatView animated:YES];
}

@end

TableViewCell.m

@interface ChatCell() {
    PFObject *message;
}

@end

@implementation ChatCell

- (void)bindData:(PFObject *)message_ {
    message = message_;

    _chatImg.layer.cornerRadius = _chatImg.frame.size.width/2;
    _chatImg.layer.masksToBounds = YES;
    PFUser *lastUser = message[PF_MESSAGES_LASTUSER];
    [_chatImg setFile:lastUser[PF_USER_PICTURE]];
    [_chatImg loadInBackground];

    _chatUsername.text = message[PF_MESSAGES_DESCRIPTION];
    _chatMessage.text = message[PF_MESSAGES_LASTMESSAGE];

    NSTimeInterval seconds = [[NSDate date] timeIntervalSinceDate:message.updatedAt];
    _chatDate.text = TimeElapsed(seconds);

}

@end

3 个答案:

答案 0 :(得分:2)

这是因为您使用- registerClass:forCellReuseIdentifier:注册了单元格。

如果以这种方式注册,则必须以编程方式构建视图或在ChatCell代码中加载nib文件。

要解决此问题,请执行以下任一操作:

  • 创建一个包含表格视图单元格视图的nib文件,并将该类设置为ChatCell。然后使用- registerNib:forCellReuseIdentifier:注册笔尖。

  • 以编程方式构建视图,例如。创建UILabel并将其添加为ChatCell的子视图。

  • 在故事板中创建原型单元格,并将单元格标识符设置为ChatCell。然后删除- registerClass:forCellReuseIdentifier:

答案 1 :(得分:0)

检查故事板中是否给出了正确的单元格标识符。 (区分大小写) " ChatCell"

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"ChatCell";
    ChatCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    [cell bindData:messages[indexPath.row]];

    return cell;
}

答案 2 :(得分:0)

您正在后台线程上更新UI。在“loadMessages”方法中试试这个。

[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            //update UI here
            if (error == nil) {
                [messages removeAllObjects];
                [messages addObjectsFromArray:objects];
                [_tableMessages reloadData];
            } else [ProgressHUD showError:@"Network error."];
            [refreshControl endRefreshing];
        });

    }];