gpt4 book ai didi

ios - 在 UIView 中具有不同高度的各种自定义单元格的 UITableView

转载 作者:行者123 更新时间:2023-11-28 22:22:54 25 4
gpt4 key购买 nike

我正在构建一个基本上由以下结构组成的 IOS 应用主页 -

enter image description here

这基本上是一个包含嵌套 UIScrollView 的 UIView,后者又包含一个带有 3 个自定义单元格的 TableView。

我从动态数组中提取数据并将其过滤到相关单元格中 - 除了两个我无法解决的问题外,一切正常 -

1- Storyboard中的自定义单元格具有不同的高度,但在编译应用程序时它们始终具有相同的高度 - 是否可以在 UITableView 行上设置自动高度?如果没有,谁能解释我如何为每个单元格应用正确的高度?

2- 包含表格 View 的 TableView/View 需要扩展以使所有动态单元格可见 - 我该如何实现?

下面是我的tableview方法供引用-

-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier =@"CellFeed";
static NSString *CellIdentifierArtNo =@"CellArtRecNo";
static NSString *CellIdentifierBook =@"CellBooking";
UITableViewCell *cell;
feedData *f = [self.HpFeedArray objectAtIndex:indexPath.section];

NSString * ArtPString = @"articleP";
NSString * ArtNoPString = @"article";
NSString * ArtBook = @"booking";

if([f.FeedGroup isEqualToString:ArtPString]){
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier
forIndexPath:indexPath];
CellHp_RecArticleWithImage *cellImage = (CellHp_RecArticleWithImage *)cell;
cellImage.selectionStyle = UITableViewCellSelectionStyleNone;
cellImage.artTitle.text = f.FeedTitle;
cellImage.artImg.text = f.FeedDesc;
cellImage.artDate.text = f.FeedDate;
cellImage.textLabel.backgroundColor=[UIColor clearColor];

return cellImage;
}
if([f.FeedGroup isEqualToString:ArtBook]){
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierBook
forIndexPath:indexPath];
CellHp_BookingAlert *cellBook = (CellHp_BookingAlert *)cell;
cellBook.selectionStyle = UITableViewCellSelectionStyleNone;
cellBook.HeadlineLbl.text = f.FeedTitle;
cellBook.TextBoxLbl.text = f.FeedDesc;
//cellBook.DateLbl.text = f.FeedDate;

return cellBook;
}
else{
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierArtNo
forIndexPath:indexPath];
CellHp_RecArticleNoImage *cellNoImage = (CellHp_RecArticleNoImage *)cell;
cellNoImage.selectionStyle = UITableViewCellSelectionStyleNone;
cellNoImage.artTitle.text = f.FeedTitle;
cellNoImage.artImg.text = f.FeedDesc;
cellNoImage.artDate.text = f.FeedDate;
cellNoImage.textLabel.backgroundColor=[UIColor clearColor];

return cellNoImage;
}

干杯

最佳答案

看起来您有自定义的 UITableViewCell 子类,例如 CellHp_RecArticleNoImageCellHp_BookingAlert(我可以建议将它们重命名为不那么令人头疼的名称吗?;))。

在那种情况下,如果您只有 3 种不同的细胞类型,您可以这样做:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell* cell = [tableView cellForAtIndexPath:indexPath];

if ([cell isKindOfClass:[CellHp_BookingAlert class])
{
return 123.0;
}
else if ([cell isKindOfClass:[CellHp_RecArticleNoImage class])
{
return ...
}
...
}

或者,您可以让所有自定义类都实现一个height 方法以获得更清晰的解决方案,并且在heightForRowAtIndexPath: 中您可以只获取单元格并调用其高度方法。

回复。问题2,使用UIView's sizeToFit method正如蒂姆提到的。

关于ios - 在 UIView 中具有不同高度的各种自定义单元格的 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19834407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com