我正在努力显示每个单元格内带有标签的 UITableView。我没有看到细胞内有任何标签。这一切都与故事板正确连接。 你能发现我做错了什么吗?
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface TableViewController2 : UITableViewController {
NSArray *nameArray;
}
@end
NS_ASSUME_NONNULL_END
#import "TableViewController2.h"
#import "TableViewCell2.h"
@interface TableViewController2 ()
@end
@implementation TableViewController2
- (void)viewDidLoad {
[super viewDidLoad];
nameArray = @[@"Bob",@"Alice"];
[self.tableView registerClass:[TableViewCell2 class] forCellReuseIdentifier:@"Cell"];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return nameArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TableViewCell2 *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
// Configure the cell...
cell.nameLabel.text = nameArray[indexPath.row];
return cell;
}
@end
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface TableViewCell2 : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@end
NS_ASSUME_NONNULL_END
#import "TableViewCell2.h"
@implementation TableViewCell2
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
您需要删除此行:
[self.tableView registerClass:[TableViewCell2 class] forCellReuseIdentifier:@"Cell"];
因为您使用的是原型单元,所以它已在 StoryBoard 中注册。通过执行上述操作,您现在可以通过初始化程序再次注册它,它将忽略所有插座。