[英]Custom Table View Cell with a big image and a label
I am trying to populate a Tableview with data from an array. 我试图用来自数组的数据填充Tableview。 I am trying to make a custom table view cell that includes a big square image, same width as the screen and the height should also be the same size as the screen's width.
我正在尝试制作一个自定义表格视图单元格,其中包括一个大的正方形图像,与屏幕的宽度相同,并且高度也应与屏幕的宽度相同。 And under the image I want to add a text label.
在图像下方,我想添加一个文本标签。 (You can think of it as a very bad copy of instagram)
(您可以将其视为instagram的非常糟糕的副本)
However, the tableview only shows empty standard cells. 但是,表视图仅显示空的标准单元格。
This is my custom cell code: 这是我的自定义单元格代码:
- (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;}
and the code from the tableview: 以及来自tableview的代码:
- (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;}
Better solution is to use constraints 更好的解决方案是使用约束
Create outlets for your height width ( control drag your constraint to cell class) 为您的高度宽度创建出口(控制将您的约束拖动到单元格类)
Change it to screen size/ height on your 将其更改为屏幕尺寸/高度
cell 细胞
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
}
Don't forget to set height for row 不要忘记为行设置高度
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UIScreen .mainScreen().bounds.height;
}
Also, you can recreate it on your code programmatically, if you want 此外,如果需要,您可以以编程方式在代码上重新创建它
Hope this helps 希望这可以帮助
Where are you allocate memory for the cell.customImageView and cell.customTextLabel? 您在哪里为cell.customImageView和cell.customTextLabel分配内存? This problem should be caused by not generating customeImageView and customeTextLabel.
此问题应由未生成customeImageView和customeTextLabel引起。 you should according to the following code to modify:
您应该根据以下代码进行修改:
//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;
}
Then invoke them when initWithStyle:reuseIdentifier:
.The other thing should be noticed is that tableView should custome cell's height by implement tableView:heightForRowAtIndexPath:
然后在
initWithStyle:reuseIdentifier:
时调用它们。另一件事是,tableView应该通过实现tableView:heightForRowAtIndexPath:
单元格的高度tableView:heightForRowAtIndexPath:
I almost did what DarkHorse said. 我几乎按照DarkHorse的话做了。
I forgot to do: 我忘了做:
customImageView = [[UIImageView alloc] init];
customTextLabel = [[UILabel alloc] init];
and then set the height in 然后在
tableView:heightForRowAtIndexPath tableView:heightForRowAtIndexPath
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.