Set label for a custom cell

雨燕双飞 提交于 2019-12-13 20:46:05

问题


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

#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

Cell for row at index 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;
}

回答1:


Have you tried using [self.tableView reloadData]?




回答2:


To start with, don't create a new random instance of the cell after you have already created one from the NIB file. Change the code to this:

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

That won't guarantee it works, but at least the NIB setup of your labels won't be getting thrown away.

If it still doesn't work. Debug. Check the frames of the labels. Check the label references are set (not nil).




回答3:


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


来源:https://stackoverflow.com/questions/17052208/set-label-for-a-custom-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!