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?
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
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