当数据发现或没有时,在tableview中自定义标签文本?目标c
我试图找到一个解决方案,但到目前为止没有运气,我可以在tableview中显示一个自定义标签,其中numberOfSections
返回零,如果数组中没有数据其他我要返回所需的。当数据发现或没有时,在tableview中自定义标签文本?目标c
现在我发送一个viewDidLoad
Json请求,所以我可以在tableview中填充数据。当视图加载时,最初我得到没有数据在自定义标签中找到这很好,因为初始数组是空的,并且当我发送JSO请求时将文本更改为“获取数据请等待”,但是没有任何影响而且我经常显示没有找到数据。
你们能请大家指教我错过了什么吗?
请不要张贴numberOfRows
的解决方案作为1
和部分返回作为1
。
在此先感谢。
得到Json数组后检查这个条件。
if([YourJsonArray count]==0)
{
[self showalert:@"" msg:@"No data found"];
}
else
{
[self.tbleView reloadData];
}
警报方法
-(void)showalert:(NSString *)strTitle msg :(NSString *)message
{
UIAlertController * alert=[UIAlertController alertControllerWithTitle:strTitle
message:message
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* yesButton = [UIAlertAction actionWithTitle:@"Ok "
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
}];
[alert addAction:yesButton];
[self presentViewController:alert animated:YES completion:nil];
}
而且你可以在tableview中的numberofrows或部分按您的要求
lalit,我不想隐藏一个标签,我只是想在请求发送到“提取数据请稍候”时更改该标签的文本,如果没有找到记录,我想将文本更改为“找不到数据”。通过您的解决方案,我隐藏并取消隐藏不需要的标签。 –
你可以使用这个检查我的更新答案的警报 –
下面,供大家参考代码中使用YourJsonArray。
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSInteger numOfSections = 0;
if ([searchArray count])
{
self.tableView = UITableViewCellSeparatorStyleSingleLine;
numOfSections = 1;
self.tableView.backgroundView = nil;
}
else{
if ([results count])
{
self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
numOfSections = 1;
self.tableView.backgroundView = nil;
}
else
{
self.noDataLabel.text = @"No Data Available";
self.noDataLabel.textColor = [UIColor blackColor];
self.noDataLabel.textAlignment = NSTextAlignmentCenter;
self.tableView.backgroundView = self.noDataLabel;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
}
return numOfSections;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSUInteger rows =0;
if(self.searchBar.text.length > 0){
rows = [searchArray count];
return rows;
}
else{
rows = results.count;
}
DLog(@"%ld",rows);
return rows;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCell"];
if((self.searchBar.text.length > 0)){
sObject * searchObj = [searchArray objectAtIndex:indexPath.section] ;
[cell setData:searchObj];
}
else if([results count] == 0){
return cell;
}
else {
sObject * ordObj = [results objectAtIndex:indexPath.row];
[cell setData:ordObj];
}
return cell;
}
,我添加以下线路发送Json
请求之前
self.noDataLabel.text = @".. Fetching Data Please Wait ..";
llyas你检查填充表前阵算什么?你正在重新加载主线程上的表吗?有许多事情可以检查和解决,使用断点来调试。 –
你可以上传一些代码更清晰 –
Tushar,是的,我已经检查了你上面写的东西,一切都如预期。 –