我在应用程序中使用自定义UITableViewCell来为每行显示一个简单标签。但是,由于某种原因,我无法显示包含要在每个单元格中显示的文本的数组的内容。我使用Interface Builder在.xib文件中创建了自定义UITableViewCell,而使用此自定义UITableViewCell的viewController位于情节提要文件中。
这是我的屏幕在IB中的外观:
这是我的自定义UITableViewCell类中的相关方法:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
因为我已经在IB中完成了所有工作,所以没有在initWithStyle方法中包含任何代码。
我在使用自定义单元格的ViewController内的相关代码如下:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.menuArray = @[@"Apples", @"Pears", @"Bananas", @"Oranges", @"Peaches"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"Cell";
MenuTableCell *cell = (MenuTableCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = (MenuTableCell *)[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.menuLabel.text = [self.menuArray objectAtIndex:indexPath.row];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.menuArray count];
}
有人可以看到我在做什么错吗?
在您的viewController中实现以下所需的方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.menuArray count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.menuArray count];
}
我已经编辑了cellForRowAtIndexPath看看
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
MenuTableCell *cell = (MenuTableCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//Edited Code for creating the cell*****************************************
if (cell == nil)
{
NSArray *objectsArray = [[NSBundle mainBundle] loadNibNamed:@"MenuTableCell" owner:self options:nil];
for (id objectCell in objectsArray)
{
cell = (MenuTableCell*)objectCell;
}
}
cell.menuLabel.text = [self.menuArray objectAtIndex:indexPath.row];
return cell;
}
希望这对你有用
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句