为自定义单元格设置标签 [英] Set label for a custom cell

查看:55
本文介绍了为自定义单元格设置标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a custom table cell and I'm just trying to set the label, image, etc but for some reason it's not working.

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

Here is my code for my custom cell

BrowserMenuCell.h

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

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

推荐答案

这是您创建单元格的错误方式.试试这个:

This is the wrong way you create cell. Try this one:

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

这篇关于为自定义单元格设置标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆