【问题标题】:UITableView doesn't display the cells in IOS 7UITableView 不显示 IOS 7 中的单元格
【发布时间】:2023-04-11 10:49:01
【问题描述】:

当我尝试将我的应用从 iOS6 转换为 iOS7/iOS8 时遇到了这个问题。

UITableView 不显示单元格(在 iOS6 中工作)。

我有一个在 iOS7 中运行良好的类似视图(它没有 UISearchView

我尝试了几种解决方法,例如清除背景、删除 UISearchBar 等...

两个示例中的代码相似(单元格由- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPathmethod 生成)

我使用 Reveal 软件来分析这个层次结构。

在 XCODE 4 中为 iOS6 支持编译的代码运行良好。 (在 iphone 5s 和 iOS7 上测试)

编辑:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath   *)indexPath
{
static NSString *CellIdentifier = @"CellAloj";
AlojamentoTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if(cell==nil){
    posicao=0;
}
posicao=70+(100*indexPath.row);
[bdAl.arrayFotos removeAllObjects];    NSMutableString *simbpreco=[[NSMutableString alloc]init];
NSNumber *id1=[bdAl.idAlojamento objectAtIndex:indexPath.row];
[self drawviewRank:[id1 integerValue]];
//posicao=posicao+100;

cell.backgroundView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"aloj_cell3.png"]];
//Não aparecer mais celulas depois da última com conteúdo
UIView *footer = [[UIView alloc] initWithFrame:CGRectZero];
self.tableView1.tableFooterView = footer;
///////////////
[bdAl SelectFotos:[[bdAl.idAlojamento objectAtIndex:indexPath.row]integerValue]];
cell.nomeAloj.text=[bdAl.arrayNomeAloj objectAtIndex:indexPath.row];
cell.imgAloj.image=[[UIImage alloc]init];
UIImage *imagem=[bdAl.arrayFotos objectAtIndex:0];
cell.tipo.text=[bdAl.arrayTipoAloj objectAtIndex:indexPath.row];
cell.labelLocalidade.text=[bdAl.arrayLocalidade objectAtIndex:indexPath.row];
cell.imgAloj.image=imagem;
CALayer * l = [cell.imgAloj layer];
[l setMasksToBounds:YES];
[l setCornerRadius:10.0];
cell.detailAloj.text=[bdAl.arrayDescAloj objectAtIndex:indexPath.row];
int i=[[bdAl.arraypreco objectAtIndex:indexPath.row]integerValue];
for (int j=0; j<i; j++){
    [simbpreco appendString:@"€"];
}
[tableView1 setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];

[tableView1 setSeparatorColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"nav_menu.png"]]];
cell.preco.text=simbpreco;
return cell;

EDIT2:

故事板:

EDIT3:此方法返回 17 行:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [bdAl.arrayNomeAloj count];
}

还有numberOfSectionsInTableView 方法:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

EDIT4:

#import <UIKit/UIKit.h>
#import "AlojamentoTableViewController.h"
@interface AlojamentoTableCell : UITableViewCell{

}
@property (strong, nonatomic) IBOutlet UILabel *tipo;
@property (strong, nonatomic)IBOutlet UILabel *nomeAloj;
@property (strong, nonatomic)IBOutlet UILabel *detailAloj;
@property (strong, nonatomic)IBOutlet UIImageView *imgAloj;
@property (strong, nonatomic) IBOutlet UILabel *labelLocalidade;
@property (strong, nonatomic)IBOutlet UILabel *preco;
@end

【问题讨论】:

  • 你试过关闭自动布局吗?
  • @RezaShirazian 是的,同样的问题。
  • 你能粘贴你的 tableView:cellForRowAtIndexPath: 代码吗?
  • @JustinMoser 代码已添加。可能有点混乱。
  • “代码有效”这一事实并不能使它正确。你必须停止这样做。

标签: ios objective-c iphone uitableview hierarchy


【解决方案1】:

我解决了问题:

我有这个方法:

-(void)RemoveAllObjects{
    [bdAl.arrayLocalidade removeAllObjects];
    [bdAl.idAlojamento removeAllObjects];
    [bdAl.arrayDescAloj removeAllObjects];
    [bdAl.arrayFotos removeAllObjects];
    [bdAl.arrayMail removeAllObjects];
    [bdAl.arrayTipoAloj removeAllObjects];
    [bdAl.arrayMorAloj removeAllObjects];
    [bdAl.arrayNomeAloj removeAllObjects];
    [bdAl.arrayStar removeAllObjects];
    [bdAl.arrayTelf removeAllObjects];
    [bdAl.arraypreco removeAllObjects];
    NSArray *subviews = [self.tableView1 subviews];
    for (int i=0; i<[subviews count]; i++)
    {
       [[subviews objectAtIndex:i] removeFromSuperview];
    }
    //posicao=90;
}

在用户开始使用搜索功能时清除所有内容。此方法在 ViewWillAppear 中被调用。

我删除它,一切正常。

谢谢大家(我知道我必须检查我所有的代码。)

编辑:iOS7 中的 UISerachView 层次结构发生了变化,这就是子视图计数失败的原因。

【讨论】:

    【解决方案2】:

    我的猜测是你的约束有问题。视图正在移动/调整大小,这导致某些部分无法正确显示。显示其他部分是因为正在调整大小的视图没有裁剪,因此您自己绘制的位被放置在它们应该位于的视图之外。

    使用以下代码在不同视图周围添加边框以查看它们的位置。我怀疑你会发现它们中的许多都没有放在你认为的位置。

    要尝试的另一件事是关闭情节提要中的自动布局。

    @interface UIView (Extension)
    
    - (void)addBorder;
    @end
    
    
    @implementation UIView (Extension)
    
    - (void)addBorder
    {
        [self.layer setBorderColor: [[UIColor redColor] CGColor]];
        [self.layer setBorderWidth: 2];
    }
    
    @end
    
    • 编辑 -

    就在这行 cell.nomeAloj.text=[bdAl.arrayNomeAloj objectAtIndex:indexPath.row]; 之前添加行 assert(cell.nomeAloj); 并告诉我运行代码时会发生什么。

    【讨论】:

    • 当您为(例如)cell.nomeAloj 标签添加边框时?它出现了吗?另外,将此代码放入您的 cellForRowAtIndexPath 方法 assert(cell.nomeAloj);
    • 我加了,什么也没发生。
    • 如果断言没有触发,并且边框没有显示,那么nomeAloj 标签的大小要么为零,要么不在屏幕上。你需要找出它去了哪里以及它有多大。使用NSLog(@"%@", cell.nomeAloj); 并告诉我它输出了什么。
    • &lt;UILabel: 0x796db2e0; frame = (112 7; 151 25); text = 'Label'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = &lt;_UILabelLayer: 0x796db3a0&gt;&gt; 5 次,但我在数据库中有 17 个值。
    • 它只会显示五次,因为它只构建可见的单元格。尝试添加这一行 cell.nomeAloj.clipsToBounds = NO 并查看它的文本是否显示在单元格的某个位置。
    【解决方案3】:

    既然你说cellForRowAtIndexPath 被调用,这意味着应该生成单元格。我能想到的一件事是您的UITableViewUIView 实际上没有大小。

    所以普通的UITableView 应该可以滚动。你能滚动吗?甚至与那张桌子互动?

    【讨论】:

    • 是的,我可以滚动它,我可以看到添加了 17 个标签文本(生成单元格数量的对象数量)该标签文本在 cellForRowAtIndexPath 内部调用的另一个方法中/跨度>
    【解决方案4】:
    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        [cell setBackgroundColor:[UIColor clearColor]];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    

    【讨论】:

    • 就像我说的,这不是背景颜色。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多