简体   繁体   中英

Adding a UITableView as a subview to another UITableView

I am new to iOS development. I have a UITableView (say TableA) which is a subview of another UITableView (say TableB). I am using auto layout to position TableA in TableB. Now, this works fine on iOS8 but the auto layout breaks completely on iOS7 giving some NSInternalInconsistencyException. To get rid of that exception is to stop using AutoLayout.

I went with this problem to an experienced iOS developer and that person told me that I should't be adding a subview to a UITableView since it is a private view. That is just plain wrong he said. Now I don't exactly get why is that wrong but I couldn't ask him further. I want to understand why is it wrong.

UITableView and UICollectionView are specialized views that handle all of their own sub-view arrangement. Both use "cells" as subviews along with the main view background, etc.

I'm not sure why you have a table in a table; but I'm guessing what you are trying to achieve is to actually put TableB inside a cell of TableA.

Create a XIB of a small tableView and use it as your custom table cell. Create a subclass of UITableViewCell to handle the implementation. Somehow you'll have to provide a data source (and possibly delegate) for each "TableB" inside the cells of TableA.

You may want to ask yourself why you need a table inside a table; often a simple custom table cell can accomplish complex data display - or even a UICollectionView with a custom UICollectionViewLayout can display some very nice tabulated data.

You do not own UITableView implementation and you not supposed to add subviews into it by addSubview method since layout, subviews management and all internals done by UITableView. You can modify content of UITableView through provided extentability points in delegate and dataSource.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM