子类化UITableViewCell不允许多个单元格标签 [英] Subclassing UITableViewCell isn't allowing multiple cell labels
问题描述
我已将 UITableViewCell
子类化,因为我不是标题和副标题,而是分别需要一个标题和两个单独的字幕,一个价格和一个条件值。 我正在以编程方式构建表,而不是在storyboard中构建。我像这样设置子类:
I've subclassed UITableViewCell
because rather than a Title and subtitle, I want a title and two separate subtitles, a price and a condition value respectively. I'm building the table programmatically, not in storyboard. I set up the subclassing like so:
MatchCenterCell.h:
#import <UIKit/UIKit.h>
@interface MatchCenterCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *PriceLabel;
@property (strong, nonatomic) IBOutlet UILabel *ConditionLabel;
@end
然后尝试使用它:
MatchCenterViewController.m:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Initialize cell
static NSString *CellIdentifier = @"MatchCenterCell";
MatchCenterCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
// if no cell could be dequeued create a new one
cell = [[MatchCenterCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
tableView.separatorColor = [UIColor clearColor];
NSDictionary *currentSectionDictionary = _matchCenterArray[indexPath.section];
NSArray *top3ArrayForSection = currentSectionDictionary[@"Top 3"];
if (top3ArrayForSection.count-1 < 1) {
// title of the item
cell.textLabel.text = @"No items found, but we'll keep a lookout for you!";
cell.textLabel.font = [UIFont systemFontOfSize:12];
// price of the item
cell.detailTextLabel.text = @"";
// image of the item
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@""]];
[[cell imageView] setImage:[UIImage imageWithData:imageData]];
}
else {
// title of the item
cell.textLabel.text = _matchCenterArray[indexPath.section][@"Top 3"][indexPath.row+1][@"Title"];
cell.textLabel.font = [UIFont systemFontOfSize:14];
// price of the item
cell.PriceLabel.text = [NSString stringWithFormat:@"$%@", _matchCenterArray[indexPath.section][@"Top 3"][indexPath.row+1][@"Price"]];
cell.PriceLabel.textColor = [UIColor colorWithRed:0/255.0f green:127/255.0f blue:31/255.0f alpha:1.0f];
// condition of the item
cell.ConditionLabel.text = [NSString stringWithFormat:@"$%@", _matchCenterArray[indexPath.section][@"Top 3"][indexPath.row+1][@"Item Condition"]];
cell.ConditionLabel.textColor = [UIColor colorWithRed:0/255.0f green:127/255.0f blue:31/255.0f alpha:1.0f];
// image of the item
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:_matchCenterArray[indexPath.section][@"Top 3"][indexPath.row+1][@"Image URL"]]];
[[cell imageView] setImage:[UIImage imageWithData:imageData]];
cell.imageView.layer.masksToBounds = YES;
cell.imageView.layer.cornerRadius = 2.5;
}
return cell;
}
但是只有项目的标题和图片显示在每个单元格, PriceLabel
和 ConditionLabel
不显示。我是否错误地将其分类?
However only the title of the item and the image show in each cell, the PriceLabel
and ConditionLabel
don't show. Have I subclassed this incorrectly?
推荐答案
希望这对你有所帮助
MatchCenterCell的xib文件
if your making the xib file for MatchCenterCell
而不是这个
cell = [[MatchCenterCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];---------- (1)
你应该使用
cell = (MatchCenterCell *) [[[NSBundle mainBundle] loadNibNamed:@"MatchCenterCell" owner:self options:nil] lastObject]; -------- (2)
并使用单元格。 PriceLabel.text = @text
而不是 cell.textLabel.text
。
已编辑
您是以编程方式完成所有操作,然后使用(1)正如你已经在做的那样,
- >首先检查你是否对你的单元格元素(标签和图像)进行了alloc-init
you are making it all programmatically , then use (1) as you are already doing , ->first check you doing alloc-init of your cell element(label and image)
---你的UITableViewCell子类 - ----
--- your UITableViewCell subclass -----
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.PriceLabel = [[ UILabel alloc]initWithFrame:CGRectMake(10, 10, 30, 40)];
}
return self;
}
---你的tableview委托方法--------
--- your tableview delegate method --------
{
.....
cell.PriceLabel.text = [NSString stringWithFormat:@"$%@", _matchCenterArray[indexPath.section][@"Top 3"][indexPath.row+1][@"Price"]];
[cell.contentView addSubview:cell.PriceLabel];
.....
}
注意
: - 最佳做法是在UITableViewCell子类中提供数据。
Note
:- The best practice is to feed data in UITableViewCell subclass.
这篇关于子类化UITableViewCell不允许多个单元格标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!