簡體   English   中英

iOS增加/減少tableview的大小

[英]ios increase/ decrease the size of a tableview

我不熟悉ios,但遇到以下問題。

我想根據表格視圖中元素的數量來增加和減少表格視圖的高度。 如果在輸入時客戶端在輸出中給出了3個或3個以上的元素,則我希望看到的表格視圖比默認行大2行。 當輸出中的元素數為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屏幕上上下移動。 為什么?

為什么不在cellForRowAtIndexPath:(NSIndexPath *)indexPath 委托方法中設置邊界

- (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
}

希望這對您有所幫助。 希望我正確理解你的問題。

嘗試這個,

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

CGRect tableFrame = self.tableView.frame;
tableFrame.size.height = height;
self.tableView.frame = tableFrame;
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的框架。

為了避免tableView移動,可以使用tableFrame.origin.y = self.tableView.frame.origin.y

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM