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

3 回答 3

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;
}
于 2013-06-11T20:56:15.723 回答
0

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

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

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

如果还是不行。调试。检查标签的框架。检查标签引用是否已设置(不是零)。

于 2013-06-11T19:42:54.917 回答
0

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

于 2013-06-11T19:43:37.107 回答