[英]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
UITableViewDelegate
和UITableViewDataSource
作為已發布的代碼,或者實際上是將UITableView的委托設置為HomeVC
。 因此,您應該檢查以下內容:
使用Interface Builder或以下代碼將UITableView的數據源設置為HomeVC
:
self.tableView.dataSource = self; // I am assuming self == HomeVC instance
確保[self.NotifTotal count] > 0
。
通過在cellForRowAtIndexPath
添加一個斷點並確認它已被調用,確保它與UITableView的配置問題無關。
這看起來像是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.