gpt4 book ai didi

ios - UITableViewCell 不显示

转载 作者:行者123 更新时间:2023-11-28 21:54:51 25 4
gpt4 key购买 nike

我的 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

最佳答案

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

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

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

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

  • 以编程方式构建 View ,例如。创建一个 UILabel 并将其添加为 ChatCell 的 subview 。

  • 在 Storyboard中制作原型(prototype)单元格并将单元格标识符设置为ChatCell。然后删除 - registerClass:forCellReuseIdentifier:

关于ios - UITableViewCell 不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26926146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com