[英]Custom UITableViewCell without dequeueReusableCellWithIdentifier
So I have a custom UITableViewCell: 所以我有一个自定义的UITableViewCell:
TestTableViewCell.h TestTableViewCell.h
#import <UIKit/UIKit.h>
@interface TestTableViewCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *testCellLabel;
@end
TestTabelViewCell.m TestTabelViewCell.m
#import "TestTableViewCell.h"
@implementation TestTableViewCell
-(id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
_testCellLabel = [[UILabel alloc] init];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
And then I have view controller with a table view that uses the custom table view cell. 然后我有一个视图控制器与一个使用自定义表视图单元格的表视图。 However this issue is that I don't want to use dequeueReusableCellWithIdentifier within the cellForRowAtIndexPath.
但是这个问题是我不想在cellForRowAtIndexPath中使用dequeueReusableCellWithIdentifier。 I instead want to have an array of cells.
我想要有一个单元格数组。
ViewController.h ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@end
ViewController.m ViewController.m
#import "ViewController.h"
#import "TestTableViewCell.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSArray *myTableViewCells;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSArray *)myTableViewCells {
TestTableViewCell *cell1 = [[TestTableViewCell alloc] init];
cell1.testCellLabel.text = @"one";
cell1.backgroundColor = [UIColor blueColor];
TestTableViewCell *cell2 = [[TestTableViewCell alloc] init];
cell2.testCellLabel.text = @"two";
cell1.backgroundColor = [UIColor greenColor];
if (!_myTableViewCells) {
_myTableViewCells = @[cell1, cell2];
}
return _myTableViewCells;
}
#pragma mark - UITableView delegate functions
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.myTableViewCells.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TestTableViewCell *cell = self.myTableViewCells[indexPath.row];
return cell;
}
@end
The problem is that there is no testCellLabel
appearing in the table view cell. 问题是表视图单元格中没有出现
testCellLabel
。 I know the cells are there, because I set their background colour. 我知道细胞在那里,因为我设置了它们的背景颜色。
After talking to a few people, apparently I need to do some sort of loading from the XIB or the NIB for the UI to load properly? 在与几个人交谈之后,显然我需要从XIB或NIB进行某种加载才能正确加载UI? Even though the label is defined in the cell in the storyboard.
即使标签是在故事板中的单元格中定义的。
I know this is going against the norm and that Apple really wants you to use dequeueReusableCellWithIdentifier, but I know it won't work in the situation I need it in. I have done the reading on that much so please don't just tell me to use it. 我知道这违反常规,Apple真的希望你使用dequeueReusableCellWithIdentifier,但我知道它在我需要它的情况下不起作用。我已经做了那么多的阅读所以请不要只告诉我使用它。 This code example is just very basic for example sake and ease of use.
这个代码示例只是非常基础,例如清单和易用性。
Any help would be greatly appreciated. 任何帮助将不胜感激。
TestTableViewCell *cell1 = [[TestTableViewCell alloc] init];
Creates a new TestTableViewCell
object and does not instantiate it from the storyboard like you're thinking it does. 创建一个新的
TestTableViewCell
对象,并不像你想象的那样从故事板中实例化它。 Therefor all outlets created will be nil and simply not show up. 因此,所有创建的商店都将是零,并且根本不会显示。 The fact that you can set the background colour is not evidence that your implementation works.
您可以设置背景颜色的事实并不表明您的实现有效。
You need to use dequeueReusableCellWithIdentifier
. 您需要使用
dequeueReusableCellWithIdentifier
。 You say that it doesn't work for your problem.. show me how it doesn't work and I will tell you why you're wrong. 你说它对你的问题不起作用..告诉我它是如何工作的,我会告诉你为什么你错了。
Edit 编辑
I see in your comments you say your cell needs a custom setter. 我在你的评论中看到你说你的手机需要一个自定义的设定器。 Well, when you use
dequeueReusableCellWithIdentifier
you can do all setup work in awakeFromNib
(If using a xib file) OR initWithCoder
if you are using the storyboard. 好吧,当你使用
dequeueReusableCellWithIdentifier
你可以在awakeFromNib
(如果使用xib文件)或initWithCoder
进行所有设置工作,如果你正在使用故事板。
您可以在不使用dequeueResableCellWithIdentifer
情况下创建单元格。
[[UITableViewCell alloc]initWithStyle:<#UITableCellStyle#> resueIdentifier:<#(nullable *NSString)#>]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.