Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何限制UITableview显示的单元格数?_Iphone_Uitableview - Fatal编程技术网

Iphone 如何限制UITableview显示的单元格数?

Iphone 如何限制UITableview显示的单元格数?,iphone,uitableview,Iphone,Uitableview,我使用tableview在我正在使用的测验应用程序中显示一些信息。我的问题是如何使tableview只显示所需的单元格数量。我将委托方法的行数设置为: -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 5; } 但在表视图的底部是不需要的空单元格。如果我将tableview样式设置为grouped,我将得到5个单元格,并且在它们下面没有空单元格

我使用tableview在我正在使用的测验应用程序中显示一些信息。我的问题是如何使tableview只显示所需的单元格数量。我将委托方法的行数设置为:

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   return 5;
}
但在表视图的底部是不需要的空单元格。如果我将tableview样式设置为grouped,我将得到5个单元格,并且在它们下面没有空单元格。我看到其他人做了这件事,但似乎无法解决。我想知道他们是否在表尾添加了一个自定义视图来取消空单元格


任何想法或帮助都值得赞赏。

您可以实现heightForRowAtIndexPath:并计算正确的高度,以便在屏幕上仅显示5个单元格。

简单的方法是缩小tableView大小。也就是说,5个单元格20个点每个给出100.0f,将高度设置为100.0f将导致只有5行可见。另一种方法是返回更多的行,但是第6行、第7行以及其他一些alpha为0的视图,但是这看起来很麻烦。您是否尝试将某些颜色视图作为footerView返回?

如果您想根据单元格数量进行调整,可以尝试更改表格视图的框架

试试这样的方法:

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   return 5;
}
[表格设置框:CGRectMake(x,y,宽度,高度*[列表计数])


高度是指单元格的高度。

一种不需要调整单元格大小的更好方法是关闭默认分隔符(将样式设置为“无”),然后在单元格中设置分隔线。

正如Nyx0uf所说,限制单元格大小可以实现这一点。例如:

- (CGFloat)tableView:(UITableView *)tabelView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat result;

    result = 100;

    return result;
}
return [postListData count];

我遇到了一个类似的问题,如何只为包含数据的单元格显示分隔符

我所做的是:

UIView *separatorView = [[UIView alloc] initWithFrame:CGRectMake:
                                        (0, cell.frame.size.height-1, 
                                           cell.frame.size.width, 1)];
[separatorView setBackgroundColor:[UIColor lightGrayColor]];
[separatorView setAlpha:0.8f];
[cell addSubView:separatorView];
  • 禁用整个tableView的分隔符。你可以在教室里做 在Interface builder中或通过调用
    [yourTableView设置分隔符样式:UITableViewCellSeparatorStyleNone]

  • cellforrowatinexpath
    中,使用单元格填充表格视图,创建一个新的
    UIView
    并将其设置为单元格的子视图。此视图的背景为浅灰色,略微透明。您可以通过以下方法实现此目的:

    UIView *separatorView = [[UIView alloc] initWithFrame:CGRectMake:
                                            (0, cell.frame.size.height-1, 
                                               cell.frame.size.width, 1)];
    [separatorView setBackgroundColor:[UIColor lightGrayColor]];
    [separatorView setAlpha:0.8f];
    [cell addSubView:separatorView];
    
    此视图的宽度为1像素,与默认分隔符相同,它在底部延伸单元格的长度

  • 由于
    cellForRowAtIndexPath
    仅在
    numberOfRowsInSection
    中指定的频率下调用,因此这些子视图将仅为具有数据且应具有分隔符的单元格创建


    希望这能有所帮助。

    您总是要有5行吗?如果是动态情况,则应根据tableview的数据源设置行数。例如:

    - (CGFloat)tableView:(UITableView *)tabelView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        CGFloat result;
    
        result = 100;
    
        return result;
    }
    
    return [postListData count];
    
    这将返回数组中保存内容的记录的计数

    tableview只显示您告诉它的行数和节数。如果总是只有一个部分,不要实现下面的方法

        - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 2;
    }
    

    如果没有此选项,tableview将只有1个部分。正如您所想象的,使用它,您可以指定分区的数量。

    这对我来说很有效-我在iphone 5的屏幕底部有额外的空行-

    在我的例子中,我需要9行

    - (CGFloat)tableView:(UITableView *)tabelView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {        
        return self.tableView.frame.size.height / 9;
    }
    

    在UITableViewController中实现以下两种方法:

    - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
    {
        if (section == tableView.numberOfSections - 1) {
            return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
        }
        return nil;
    }
    
    
    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
    {
        if (section == tableView.numberOfSections - 1) {
            return 1;
        }
        return 0;
    }
    

    事实上,这些代码告诉tableview,您不再需要为我呈现分隔符行,这样看起来就不会显示空单元格(事实上,也不能选择空单元格)

    如果您想保留分隔符,可以插入一个伪页脚视图。这将限制tableview仅显示在tableview:numberOfRowsInSection:

    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
    迅速:

    self.tableView.tableFooterView = UIView(frame: CGRectZero)
    

    这很简单。只需如下设置popover的大小: self.optionPickerPover.popoverContentSize=CGSizeMake(200200);
    当然,您可以根据内容的大小和行数调整大小(200200)。

    不清楚您是想避免使用分组样式还是在使用时遇到问题?我在标准UITableView上实现这两种分组样式都没有任何问题。我想说的是,当我运行应用程序时,如果我使用分组并将每个部分的行数设置为5,我会看到5行,就是这样,只有5行。另一方面,如果我使用标准表视图并将行数设置为5行,我看到我得到了我的5行,但在它们下面是额外数量的空行,这些空行没有内容,也不需要,但是放在那里只是为了填满屏幕。因此,这需要在IB中创建一个自定义单元格,或者我必须以编程方式在单元格中添加一个分隔符?您可以这样做。我通常将UITableViewCell子类化,但您可以将子视图添加到普通UITableViewCell的contentview中。IB或代码完全取决于您喜欢如何工作……不错。只需确保不要为该表视图设置固定高度,否则额外的页脚视图可能会在表视图下进一步向下推(即使它只是一个虚拟视图)。在Swift 3中:self.tableView.tableFooterView=UIView(frame:CGRect.zero)