设置自定义单元格的标签

我有一个自定义表格单元格,我只是试图设置标签,图像等,但由于某种原因,它不工作。

这是我的自定义单元格的代码

BrowserMenuCell.h

#import <UIKit/UIKit.h> @interface BrowseMenuCell : UITableViewCell @property (strong, nonatomic) IBOutlet UIButton *wishListBUtton; @property (strong, nonatomic) IBOutlet UIImageView *itemImage; @property (strong, nonatomic) IBOutlet UILabel *itemLabel; @property (strong, nonatomic) IBOutlet UILabel *itemPrice; @property (strong, nonatomic) IBOutlet UITextField *quantityField; @property (strong, nonatomic) IBOutlet UILabel *totalLabel; - (IBAction)plusButton:(id)sender; - (IBAction)cartButton:(id)sender; - (IBAction)minusButton:(id)sender; @end 

BrowserMenuCell.m

 #import "BrowseMenuCell.h" @implementation BrowseMenuCell - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // Initialization code } return self; } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } - (IBAction)plusButton:(id)sender { } - (IBAction)cartButton:(id)sender { } - (IBAction)minusButton:(id)sender { } @end 

在索引path的行的单元格

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { BrowseMenuCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ItemsCell"]; OfficeSupply *supply = [_items objectAtIndex:indexPath.row]; if(cell == nil) { NSArray * views = [[NSBundle mainBundle] loadNibNamed:@"BrowseMenuCell" owner:nil options:nil]; for (id obj in views){ if([obj isKindOfClass:[UITableViewCell class]]) { BrowseMenuCell * obj = [[BrowseMenuCell alloc]init]; obj.itemLabel.text = supply.itemName; cell = obj; break; } } } return cell; } 

你有没有尝试过使用[self.tableView reloadData]?

首先,在你已经从NIB文件创build一个单元之后,不要创build一个新的单元的随机实例。 将代码更改为:

  for (BrowseMenuCell *obj in views){ if([obj isKindOfClass:[BrowseMenuCell class]]) { obj.reuseIdentifier = @"ItemsCell"; obj.itemLabel.text = supply.itemName; cell = obj; break; } } 

这不能保证它的工作,但至less标签的NIB设置不会被扔掉。

如果它仍然不起作用。 debugging。 检查标签的框架。 检查标签参考设置(而不是无)。

这是你创build单元格的错误方式。 试试这个:

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { BrowseMenuCell *cell = (BrowseMenuCell*)[tableView dequeueReusableCellWithIdentifier:@"ItemsCell"]; OfficeSupply *supply = [_items objectAtIndex:indexPath.row]; if (_cellObj == nil) { [[NSBundle mainBundle] loadNibNamed:@"BrowseMenuCell" owner:nil options:nil]; } cell.itemlabel.text = supply.itemName; return cell; }