Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.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
ios增加/减少tableview的大小_Ios_Objective C_Tableview - Fatal编程技术网

ios增加/减少tableview的大小

ios增加/减少tableview的大小,ios,objective-c,tableview,Ios,Objective C,Tableview,我是ios新手,我有以下问题 我想根据表格视图中的元素数量增加和减少表格视图的高度大小。如果在输入时,客户端在输出时给出3个或3个以上的元素,我希望看到一个比默认值大2行的tableview。当输出的元素数为2或1时,我将看到默认的表视图高度。宽度将是相同的。如何做到这一点 这是我的密码: if (inputList.count>2) { CGRect bounds = [self.tableView bounds]; [self.table

我是ios新手,我有以下问题

我想根据表格视图中的元素数量增加和减少表格视图的高度大小。如果在输入时,客户端在输出时给出3个或3个以上的元素,我希望看到一个比默认值大2行的tableview。当输出的元素数为2或1时,我将看到默认的表视图高度。宽度将是相同的。如何做到这一点

这是我的密码:

if (inputList.count>2)
    {
           CGRect bounds = [self.tableView bounds];
         [self.tableView setBounds:CGRectMake(bounds.origin.x,
                                        bounds.origin.y,
                                        bounds.size.width,
                                        bounds.size.height + 50)];
         CGRect tableFrame = [ self.predictiveDialog frame];
               tableFrame.size.height=75;
         [self.tableView setFrame:tableFrame];

    }
    else 
    {
         NSLog(@"decrease size");

         [self.tableView setBounds:CGRectMake(bounds.origin.x,
                                                    bounds.origin.y,
                                                    bounds.size.width,
                                                    bounds.size.height - 50)];
         CGRect tableFrame = [ self.predictiveDialog frame];
         tableFrame.size.height=44;
         [self.tableView setFrame:tableFrame];

    }
在这种情况下,输入元素时,tableview的宽度和高度正常,但取决于inputlist中元素的数量-tableview在ios屏幕上上上下移动。为什么?

试试这个

CGFloat height = self.tableView.rowHeight;
height *= inputList.count;

CGRect tableFrame = self.tableView.frame;
tableFrame.size.height = height;
self.tableView.frame = tableFrame;

为什么不在
cellforrowatinexpath:(nsindepath*)indepath
委托方法中设置界限

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if([indexPath row]>2)
    {

         [tableView setBounds:CGRectMake(self.tableView.frame.origin.x,
                                        self.tableView.frame.origin.y,
                                        self.tableView.frame.size.width,
                                        self.tableView.frame.size.height + 50)];

    }

  // your other code
}
希望这对你有帮助。希望我能正确理解你的问题

CGRect bounds = [self.tableView bounds];
[self.tableView setBounds:CGRectMake(...)];
CGRect tableFrame = [ self.predictiveDialog frame];
tableFrame.size.height=75;
[self.tableView setFrame:tableFrame];
代码
[self.tableView setBounds:CGRectMake(…)]
在这里是无用的,因为您最终设置了tableView的框架


为了避免表视图移动,您可以使用
tableFrame.origin.y=self.tableView.frame.origin.y

是否要增加表视图的边框或表视图中单元格的高度?我想增加表视图的高度。请参考我的答案并相应地设置边界。代码中的“界限”指的是什么?我指的是哪种观点。我猜这是另一个视图我编辑了代码。我指的是我的tableView边界。谢谢你的评论。我已经测试过了,但是每次我输入越来越多的元素,tableview就会出现在ios屏幕的底部。