2013-06-11 49 views
0

我有一個自定義表格單元格,我只是試圖設置標籤,圖像等,但由於某種原因,它不工作。自定義單元格的設置標籤

這裏是我的我的自定義單元代碼

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 

在指數路徑爲細胞排

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

刪除此行:BrowseMenuCell * obj = [[BrowseMenuCell alloc] init];並再試一次 – Wain

+0

然後我不能設置itemLabel.text – Claud

+0

我修正它的方式只是通過UILabel * name =(UILabel *)[cell viewWithTag:6];但不知道這是否正確。 – Claud

回答

0

要首先,d在你已經從NIB文件創建一個單元之後,不要創建一個新的單元的隨機實例。代碼改成這樣:

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

這並不能保證它的工作原理,但至少你的標籤的NIB設置將不會得到扔掉。

如果它仍然不起作用。調試。檢查標籤的框架。檢查標籤參考設置(而不是無)。

+0

是的,這不起作用,我只是用每個項目的標籤。 – Claud

+0

這表明您的XIB屬性引用不正確(或可能未連接)。 – Wain

0

您是否嘗試過使用[self.tableView reloadData]?

+0

我的表格正在加載,只是標籤沒有設置。 – Claud

0

這是您創建單元格的錯誤方式。試試這個:

-(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; 
} 
相關問題