我有一个uitableview,使用hpple从web加载一些数据。我已经将单元格设置为" loading ..."用这种方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if ([mealInfo isEqualToString:@"Lunch"] || [mealInfo isEqualToString:@"Dinner"] || [mealInfo isEqualToString:@"Latenight"]) {
switch (section) {
case 0:
if (_deli == nil || _deli.count == 0) {
return 1;
}
else {
return _deli.count;
}
break;
case 1:
if (_entrees == nil || _entrees.count == 0) {
return 1;
}
else {
return _entrees.count;
}
break;
}
}
这个
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
if (_deli == nil) {
cell.textLabel.text = @"Loading...";
}
else if (_deli.count > 0) {
Items *thisDeli = [_deli objectAtIndex:indexPath.row];
cell.textLabel.text = thisDeli.title;
}
else
cell.textLabel.text = @"No Data Available";
}
else if (indexPath.section == 1) {
if (_entrees == nil) {
cell.textLabel.text = @"Loading...";
}
else if (_entrees.count > 0) {
Items *thisEntree = [_entrees objectAtIndex:indexPath.row];
cell.textLabel.text = thisEntree.title;
}
else
cell.textLabel.text = @"No Data Available";
}
}
如果没有该部分的数据,我可以将该部分的标题设置为0,而不是将其设置为无可用数据。
我尝试过使用此
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//NSLog(@"%ld", (long)[tableView.dataSource tableView:tableView numberOfRowsInSection:section]);
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if ([cell.textLabel.text isEqualToString:@"No Data Available"]) {
return 0;
} else {
return 30.0f;
}
}
但它还没有奏效?
这就是我加载数据的方式
-(void)getEntrees:(NSData*)entreeData {
TFHpple *Parser = [TFHpple hppleWithHTMLData:entreeData];
// 3
NSString *XpathQueryString = self.entreeString;
NSArray *Nodes = [Parser searchWithXPathQuery:XpathQueryString];
// 4
NSMutableArray *newNodes = [[NSMutableArray alloc] initWithCapacity:0];
for (TFHppleElement *element in Nodes) {
// 5
Items *item = [[Items alloc] init];
[newNodes addObject:item];
// 6
item.title = [[element firstChild] content];
item.title = [[[element firstChild] content]stringByReplacingOccurrencesOfString:@"\n" withString:@""];
// 7
item.url = [element objectForKey:@"href"];
}
// 8
_entrees = newNodes;
[self.tableView reloadData];
}
提前感谢您的帮助!!!
答案 0 :(得分:0)
您可能错误地复制了代码?但如果没有,1)你需要在cellForRowAtIndexPath
中出列一个单元格。将这些行移动到该方法的开头:
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
2)在heightForHeaderInSection
中,您正在测试单元格标签文本但使用刚刚出列的单元格 - 这将是未配置的,因此不会有您要查找的文本。但是你不应该使用单元格标签文本来确定标题高度。使用与cellForRowAtIndexPath
中使用的逻辑相同的逻辑。所以在heightForHeaderInSection
中,使用:
if (section == 1) {
if (_entrees != nil && _entrees.count == 0 ) {
return 0;
} else {
return 30.0f;
}
}
并修改numberOfRowsInSection
如果_entrees.count为0则返回0,如下所示:
switch (section) {
case 0:
if (_deli == nil || _deli.count == 0) {
return 1;
}
else {
return _deli.count;
}
break;
case 1:
if (_entrees == nil /* don't need this --> || _entrees.count == 0*/) {
return 1;
}
else {
return _entrees.count;
}
break;
}
答案 1 :(得分:0)
这应该复制并粘贴
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
if (_deli == nil) {
cell.textLabel.text = @"Loading...";
}
else if (_deli.count > 0) {
Items *thisDeli = [_deli objectAtIndex:indexPath.row];
cell.textLabel.text = thisDeli.title;
}
else
cell.textLabel.text = @"No Data Available";
}
else if (indexPath.section == 1) {
if (_entrees == nil) {
cell.textLabel.text = @"Loading...";
}
else if (_entrees.count > 0) {
Items *thisEntree = [_entrees objectAtIndex:indexPath.row];
cell.textLabel.text = thisEntree.title;
}
else
cell.textLabel.text = @"No Data Available";
}
}