5

Tôi thực sự tạo một UIScrollView với một UITableView bên trong nó bằng trình tạo giao diện. Tôi thiết lập kích thước nội dung của tôi UIScrollView với:Vấn đề kích thước nội dung cho UITableView trong UIscrollView

scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height); 

và tôi đặt quan điểm chủ sở hữu tập tin của tôi để các UIScrollView

Tôi tạo ra một IBOutletUITableView và tôi đặt nó trong xây dựng giao diện.

Khi chế độ xem được tải. nó hiển thị kích thước nội dung chính xác cho số UIScrollView của tôi. Nhưng nó không hiển thị kích thước nội dung chính xác cho số UITableView của tôi. Bên cạnh đó khi tôi thay đổi kích thước nội dung của UIScrollView của tôi, nó thay đổi kích thước nội dung của tôi UITableView như thể cả hai kích thước nội dung có liên quan.

Bất kỳ ai biết cách tôi có thể giữ kích thước nội dung của chế độ xem bảng mà tôi đã đặt trong trình tạo giao diện?

Trả lời

12

A UITableViewUIScrollView. Có rất ít lần hữu ích khi nhúng một số UITableView vào trong một số UIScrollView.

Giả sử bạn thực sự muốn điều này, bạn sẽ cần phải làm như sau ở đâu đó. Có thể trong số UIViewController của bạn theo các phương thức như viewDidAppear: hoặc ở đâu đó sau khi bạn điền vào chế độ xem bảng.

// Set the size of your table to be the size of it's contents 
// (since the outer scroll view will handle the scrolling). 
CGRect tableFrame = tableView.frame; 
tableFrame.size.height = tableView.contentSize.height; 
tableFrame.size.width = tableView.contentSize.width; // if you would allow horiz scrolling 
tableView.frame = tableFrame; 

// Set the content size of your scroll view to be the content size of your 
// table view + whatever else you have in the scroll view. 
// For the purposes of this example, I'm assuming the table view is in there alone. 
scrollView.contentSize = tableView.contentSize; 
+0

Nó vẫn không hoạt động ... Thực ra khi tôi NSLog tableView.frame nó hiển thị tôi (null). Tôi không hiểu ... – kschaeffler

+0

Thực ra, chủ sở hữu tệp của tôi là đại biểu của UIScrollView và UITableView là một scrollView. Đó có phải là vấn đề? – kschaeffler

+0

Tôi nên rõ ràng hơn. 'tableView' và' scrollView' được coi là IBOutlets mà bạn đã liên kết với các mục đó. – DBD

0

Nếu bạn vẫn gặp sự cố, hãy thử điều này mà không cần cuộn scrollView.

Sau khi chơi đùa với ví dụ DBD của cho một lúc, tôi thấy rằng khung và contentSize dường như không có khả năng được thiết lập cùng thích:

self.tableView.contentSize = CGSizeMake(320, scrollHeight); 
self.tableView.frame = CGRectMake(self.tableView.frame.origin.x,44,self.tableView.contentSize.width,self.tableView.contentSize.height); 

Nỗ lực là để thiết lập một chiều cao tối đa của nhưng vẫn giữ khả năng cuộn qua tất cả nội dung nếu có một số lượng lớn ô. Lỗi này dường như hoạt động tốt và có thể được sửa đổi với nhiều điều kiện hơn nếu cần:

int cellCount = [YOURARRAY count]; 
CGFloat scrollHeight = cellCount*44+44;//for a cell with height of 44 and adding 44 if you have a toolbar at the bottom 

self.tableView.contentSize = CGSizeMake(320, scrollHeight);//you can change the width and or let the .xib control the autoresizing 

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad && cellCount < 14) 
{ 
    //here's were you can add more conditions if you're in landscape or portrait, but this handles 14 cells with a heightForHeaderInSection of 46 in landscape 
    self.tableView.frame = CGRectMake(self.tableView.frame.origin.x,44,self.tableView.contentSize.width,scrollHeight); 
} 
else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && cellCount < 7) 
{ 
    //same as above but it's for the iPhone in portrait 
    self.tableView.frame = CGRectMake(self.tableView.frame.origin.x,44,self.tableView.contentSize.width,scrollHeight); 
} 

Điều này làm việc chắc chắn. Bạn có thể cần phải điều chỉnh autoresizingMask bên trong của bạn .xib hoặc trong mã của bạn, nhưng hack này là giải pháp duy nhất tôi thấy rằng sẽ chăm sóc của tất cả các biến khác nhau trong trường hợp của tôi.

1

Ví dụ để tự động quản lý Chiều cao TableView cùng với scrollview, làm việc tại Swift:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     // Custom Cell 
     let cell:TblPortCell = self.tableList.dequeueReusableCellWithIdentifier("cell") as! TblPortCell 

     //tableList is the OutLet for the table 
     tableList.sizeToFit() 
     // Set the size of the table to be the size of it's contents 
     // (since the outer scroll view will handle the scrolling). 
     let height = tableList.frame.size.height 
     let pos = tableList.frame.origin.y 
     let sizeOfContent = height + pos + 130 
     //scrollView is the Outlet for the Scroll view 
     scrollView.contentSize.height = sizeOfContent 
     scrollView.sizeToFit() 

     cell.selectionStyle = UITableViewCellSelectionStyle.None 
     return cell 
    } 

Tài liệu tham khảo để:

0

Gọi dưới phương pháp nhiều lần. Mỗi lần bạn gọi sẽ có nhiều ô hơn.

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

scrollView.contentSize = tableView.contentSize; 
Các vấn đề liên quan