简体   繁体   中英

Unable to view UITableViewCell when using custom cell's in iOS

I am using a custom UITableViewCell in my application to display a simple label for each row. However, for some reason I am unable to display the contents of the array that contains the text I want to display in each cell. I made my custom UITableViewCell in a .xib file using Interface Builder, and my viewController that is using this custom UITableViewCell is inside my storyboard file.

Here is what my screen looks like in IB:

在此输入图像描述

在此输入图像描述

在此输入图像描述

Here is the relevant method inside my custom UITableViewCell class:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

Because I've done everything inside IB, I didn't include any code inside the initWithStyle method.

My relevant code inside my ViewController where I am using the custom cell is as follows:

- (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];
}

Can anyone see what it is I am doing wrong?

If you make a cell in a xib file, you should call registerNib:forIdentifier: in viewDidLoad. If you do this, it's unnecessary to check for a nil cell in cellForRowAtIndexPath.

I Think This Will Work for You.... And Also Check your Label Outlet Connection For Displaying Proper Answer

 - (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];  
}

Add numberOfRowsInSection method in yourViewController like below

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.menuArray count];
}

Implement the required methods below in your viewController

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.menuArray count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{

    return [self.menuArray count];
}

And I have edited the cellForRowAtIndexPath have a look at it

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

Hope this works for you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM