繁体   English   中英

目标c-表格视图无法在屏幕上显示数据

[英]objective c - table view not able to show the data in the screen

我有一个数据数组。 而且wr我的数据没有显示在屏幕上。 不知道,我缺少什么。

@property NSMutableArray *NotifTotal;

@interface HomeVC ()<UITableViewDelegate, UITableViewDataSource>
@end

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.NotifTotal count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"FilterTableViewCell";
    FilterTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


    NSDictionary *dict;
    dict = [self.NotifTotal objectAtIndex:indexPath.row];
    NSLog(@"%@", dict);  // data is coming.
    NSString* salt = [dict objectForKey:@"salt"];
    NSString* name = [dict objectForKey:@"Name"];
    NSLog(@"%@%@", name,swerk); // in console i can print the data
    cell.sLabel.text = [NSString stringWithFormat: @"%@", salt];
    cell.nLabel.text = [NSString stringWithFormat: @"%@", name];
    return cell;
}

为什么我的数据没有显示在屏幕上。我在屏幕上也添加了代表和数据源。任何解决方案?

您说过“我也在屏幕上添加了委托,数据源”,但对我来说还不是很清楚,这意味着要将HomeVC UITableViewDelegateUITableViewDataSource作为已发布的代码,或者实际上是将UITableView的委托设置为HomeVC 因此,您应该检查以下内容:

  • 使用Interface Builder或以下代码将UITableView的数据源设置为HomeVC

     self.tableView.dataSource = self; // I am assuming self == HomeVC instance 
  • 确保[self.NotifTotal count] > 0

  • 通过在cellForRowAtIndexPath添加一个断点并确认它已被调用,确保它与UITableView的配置问题无关。

    • 如果不是,请返回上面的2点。
    • 如果是:这是UI问题,让我们检查单元格的高度是否接近0或它们是否具有透明颜色,等等。 您可以使用Xcode的View Debugging工具来调试问题。

既然您还没有提到已经注册了单元标识符,那么我认为这就是问题所在。 一种方法是在故事板或xib中。 在表视图中选择原型单元,然后设置“标识符”字段(在Interface Builder的“属性”检查器窗格中)。 将其设置为“ FilterTableViewCell”。

原型单元的标识符字段

这看起来像是xib问题。 我在中间添加了一些代码。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"FilterTableViewCell";
FilterTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

//Add this part
if (cell == nil)
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"FilterTableViewCell" owner:self options:nil];
    cell = [nib objectAtIndex:0];
}
//end


NSDictionary *dict;
dict = [self.NotifTotal objectAtIndex:indexPath.row];
NSLog(@"%@", dict);  // data is coming.
NSString* salt = [dict objectForKey:@"salt"];
NSString* name = [dict objectForKey:@"Name"];
NSLog(@"%@%@", name,swerk); // in console i can print the data
cell.sLabel.text = [NSString stringWithFormat: @"%@", salt];
cell.nLabel.text = [NSString stringWithFormat: @"%@", name];
return cell;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM