关于ios:具有大图像和标签的自定义表格视图单元格

Custom Table View Cell with a big image and a label

我正在尝试用来自数组的数据填充Tableview。我正在尝试制作一个自定义表格视图单元格,其中包括一个大的正方形图像,与屏幕的宽度相同,并且高度也应与屏幕的宽度相同。在图像下方,我想添加一个文本标签。 (您可以将其视为instagram的非常差的副本)

但是,表视图仅显示空的标准单元格。

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {                
        customImageView.contentMode = UIViewContentModeScaleAspectFit;

        CGRect frame;
        frame.origin.x=0;
        frame.origin.y=0;
        frame.size.width=self.frame.size.width;
        frame.size.height=self.frame.size.width;

        customImageView.frame=frame;

        [customTextLabel sizeToFit];

        frame.origin = CGPointMake(0, (self.frame.size.width+1));
        customTextLabel.frame = frame;

        customTextLabel.numberOfLines = 0;
        customTextLabel.lineBreakMode = NSLineBreakByCharWrapping;

        [self.contentView addSubview:customImageView];
        [self.contentView addSubview:customTextLabel];

        [self.contentView sizeToFit]; } return self;}

和表视图中的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyCellIdentifier = @"MyCellIdentifier";

//UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:MyCellIdentifier];

FeedTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:MyCellIdentifier];


if(cell == nil) {
    cell = [[FeedTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyCellIdentifier];
}


if(!emptyTable){

     Log *log = [self.feedPosts objectAtIndex:indexPath.section];

    cell.customImageView.image = [self loadImage:(int)log.logID];

    cell.customTextLabel.text = log.logDescription;


}
return cell;}


我几乎按照了DarkHorse的话做。

我忘了做:

1
2
3
customImageView = [[UIImageView alloc] init];

customTextLabel = [[UILabel alloc] init];

,然后在

中设置高度

tableView:heightForRowAtIndexPath


您在哪里为cell.customImageView和cell.customTextLabel分配内存?此问题应由未生成customeImageView和customeTextLabel引起。您应该根据以下代码进行修改:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//Setter/Getter
//allocate memory and init customImageView/customTextLable

- (UIImageView *)customeImageView{
    if(!_ customImageView)_customImageView = [UIImageView new];

   return _customImageView;

}

- (UILable *)customTextLabel{
   if(!_customTextLable) _customTextLabel = [UIlable new];

    return _cusomTextLable;
}

然后在initWithStyle:reuseIdentifier:时调用它们。另一件事应该注意,tableView应该通过工具tableView:heightForRowAtIndexPath:

自定义单元格的高度


更好的解决方案是使用约束

  • 创建如下图所示的约束
  • enter

    1
    2
    3
    4
    5
    6
     override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TableViewCell
        cell.heightConstraint.constant = UIScreen .mainScreen().bounds.height;
        cell.widthConstraint.constant = UIScreen.mainScreen().bounds.width;
        return cell
    }

    别忘了为行

    设置高度

    1
    2
    3
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return UIScreen .mainScreen().bounds.height;
    }

    此外,如果需要,您可以通过编程方式在代码上重新创建它。

    希望这会有所帮助