Why do my prototype cells show up blank?

ぃ、小莉子 提交于 2019-12-08 08:08:16

问题


In a uitableview in my storyboard, I have defined a prototype cell. In that cell (a custom class kzTexturedCellv2) I have a text view and an image view. I have also given the cell an identifier ("kzTexturedCellv2") and used

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

kzTexturedCellv2 *cell = [[kzTexturedCellv2 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"kzTexturedCellv2"];

...

to create the cells. The problem is that the cells show up blank (without any of the subviews that I added in storyboad.) When I try to set text of the uitextview inside the cell, it always returns null. What is going on here?


回答1:


When using storyboards there is no need to alloc init the cell.

See dequeueReusableCellWithIdentifier behavior changed for prototype cells?

Use only:

kzTexturedCellv2 *cell = [tableView dequeueReusableCellWithIdentifier:@"kzTexturedCellv2"];

Alloc init will be handled by the framework




回答2:


Can you plz see the following code. I hope it will be helpful to you

- (void)viewDidLoad{
[super viewDidLoad];

// Create your image
UIImage *image = [UIImage imageNamed: @"person_menu.png"];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc]
                              initWithImage:image
                              style:UIBarButtonItemStylePlain
                              target:nil
                              action:nil];
// set the text view to the image view
self.navigationItem.leftBarButtonItem = barButton;

menuItems = @[@"title", @"Payment", @"History",@"BookNow",@"Help", @"Promotions",@"Notifications", @"Settings", @"logOut"];
}
#pragma mark
#pragma mark - UITableViewDelegate Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
// Return the number of sections.
return 1;
}

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
// Return the number of rows in the section.
return [menuItems count];
}
 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == 0) {
    return 140;
}else{
     return  75;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.highlighted = YES;
return cell;
}

What ever the identifiers you have in UITableviewCell you can collect those and add it into one NSArray

For you're case just you can use like this::

kzTexturedCellv2 *cell = [tableView dequeueReusableCellWithIdentifier:@"kzTexturedCellv2"];


来源:https://stackoverflow.com/questions/10779155/why-do-my-prototype-cells-show-up-blank

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!