简体   繁体   English

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

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

I have an array of data. 我有一个数据数组。 And wr my data are not displaying in the screen. 而且wr我的数据没有显示在屏幕上。 Not sure, what i am missing. 不知道,我缺少什么。

@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;
}

Why my data is not showing in my screen.I added the delegate, data source also in my screen.Any solution ? 为什么我的数据没有显示在屏幕上。我在屏幕上也添加了代表和数据源。任何解决方案?

You said "I added the delegate, data source also in my screen" but it is not very clear to me by that you meant conforming your HomeVC to UITableViewDelegate and UITableViewDataSource as your posted code or you actually set the delegate of your UITableView to HomeVC . 您说过“我也在屏幕上添加了委托,数据源”,但对我来说还不是很清楚,这意味着要将HomeVC UITableViewDelegateUITableViewDataSource作为已发布的代码,或者实际上是将UITableView的委托设置为HomeVC So here are something you should check: 因此,您应该检查以下内容:

  • Set datasource of your UITableView to HomeVC using Interface Builder or following code: 使用Interface Builder或以下代码将UITableView的数据源设置为HomeVC

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

  • Make sure it is not about UITableView's configuration issue by adding a break point to cellForRowAtIndexPath and confirm it called. 通过在cellForRowAtIndexPath添加一个断点并确认它已被调用,确保它与UITableView的配置问题无关。

    • If it isn't: go back to 2 points above. 如果不是,请返回上面的2点。
    • If it is: this is UI issue, let's check if your cells's height is near 0 or they have a transparent color and so on. 如果是:这是UI问题,让我们检查单元格的高度是否接近0或它们是否具有透明颜色,等等。 You can use Xcode's View Debugging tool to debug the issue. 您可以使用Xcode的View Debugging工具来调试问题。

Since you haven't mentioned having registered the cell identifier yet, I assume that's the problem. 既然您还没有提到已经注册了单元标识符,那么我认为这就是问题所在。 One way to do this is in your storyboard or xib. 一种方法是在故事板或xib中。 Select the prototype cell in your tableview, and set the "identifier" field (in the Attributes inspector pane of Interface Builder). 在表视图中选择原型单元,然后设置“标识符”字段(在Interface Builder的“属性”检查器窗格中)。 Set it to "FilterTableViewCell". 将其设置为“ FilterTableViewCell”。

原型单元的标识符字段

This looks like a xib problem. 这看起来像是xib问题。 I added a bit of code in the middle. 我在中间添加了一些代码。

- (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