在iOS中使用自定义单元格时无法查看UITableViewCell

问题描述 投票:0回答:4

我在我的应用程序中使用自定义UITableViewCell来显示每行的简单标签。但是,由于某种原因,我无法显示包含我想在每个单元格中显示的文本的数组的内容。我使用Interface Builder在.xib文件中创建了自定义UITableViewCell,而使用此自定义UITableViewCell的viewController在我的storyboard文件中。

这是我的屏幕在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];
}

谁能看到我做错了什么?

ios uitableview
4个回答
0
投票

在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;
}

希望这对你有用


1
投票

如果在xib文件中创建单元格,则应在viewDidLoad中调用registerNib:forIdentifier :.如果这样做,则无需在cellForRowAtIndexPath中检查nil单元格。


1
投票

我认为这将为您工作....并检查您的标签插座连接以显示正确的答案

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:  (NSInteger)section
{
      return [menuArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
      static NSString *simpleTableIdentifier = @"CustomCell";

       CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
     if (cell == nil)
   {
       NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
       cell = [nib objectAtIndex:0];
   }
     cell.menuLabel.text = [self.menuArray objectAtIndex:indexPath.row];  
}

0
投票

numberOfRowsInSection中添加yourViewController方法,如下所示

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.menuArray count];
}
© www.soinside.com 2019 - 2024. All rights reserved.