Uitableview 如果numberOfRowsInSection返回计数0,则节中的默认文本
我在TableView中有两个部分,分别有各自的部分标题。numberOfRowsInSection是动态计数的&它也可能是0。因此,我希望在0行的情况下在节中的某个位置显示默认文本。Uitableview 如果numberOfRowsInSection返回计数0,则节中的默认文本,uitableview,ios6,Uitableview,Ios6,我在TableView中有两个部分,分别有各自的部分标题。numberOfRowsInSection是动态计数的&它也可能是0。因此,我希望在0行的情况下在节中的某个位置显示默认文本。 我该怎么做?(使用iOS 6,XCode-4.2)为什么不在“空区”的单元格中显示默认文本? 返回1并将默认文本放在其中,而不是返回0行。它可以是这样的: - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSIntege
我该怎么做?(使用iOS 6,XCode-4.2)为什么不在“空区”的单元格中显示默认文本? 返回1并将默认文本放在其中,而不是返回0行。它可以是这样的:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Verify is the section should be empty or not
if(emptySection == NO) {
return numberOfRowsInSection;
}
else {
return 1;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell Identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
if(emptySection && indexPath.row == 0) {
cell.textLabel.text = @"This is the default text";
}
else {
// Display the normal data
}
return cell;
}
更新
以下代码将避免在点击包含默认文本的单元格时执行任何操作
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(emptySection) {
return;
}
// Perform desired action here
}
另一种解决方案是完全阻止选择单元格:
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)path
{
if(emptySection) {
retur nil;
}
return path;
}
您希望在何处显示默认文本?我希望显示文本,而不是加载自定义单元格。现在我的问题是,尽管numberofrowsinssection函数返回零;我加载了一个空白的自定义单元格。请帮助。这是我尝试过的一个选项,但我这里的问题是,如果我加载一个单元格,允许它通过segue重定向到新的ViewController,并且需要将一些数据传递到该ViewController。所以它不起作用。无论如何,我找到了解决办法。我只返回了零行数&更改了节标题!谢谢你。