设置自定义单元格的标签

时间:2013-06-11 19:30:03

标签: ios objective-c tableview

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

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

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

3 个答案:

答案 0 :(得分:0)

首先,在从NIB文件创建单元格之后,不要创建单元格的新随机实例。将代码更改为:

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

这不能保证它有效,但至少你的标签的NIB设置不会被丢弃。

如果仍然无效。调试。检查标签的框架。检查标签引用是否已设置(不是零)。

答案 1 :(得分:0)

您是否尝试过使用[self.tableView reloadData]?

答案 2 :(得分: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;
}