簡體   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