

How to configure UITableView in a UIViewController created on a .xib file
source link: https://www.codesd.com/item/how-to-configure-uitableview-in-a-uiviewcontroller-created-on-a-xib-file.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to configure UITableView in a UIViewController created on a .xib file
I have a class like this:
@interface ExerciseLogDetails : UIViewController<UIActionSheetDelegate, UITableViewDelegate, UITableViewDataSource> {
where I am trying to display some elements followed by a UITextView. The UITextView element is created on Interface Builder. When executing this code:
- (void)viewDidLoad {
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
tableView.dataSource = self;
tableView.delegate = self;
[self.view addSubview:self.tableView];
}
a table shows, but not the one I configured in Interface Builder. It is completely blank and unformatted. How can I access my table and populate it progrmmatically with data?
Thank you!
If you configured a tableView in IB you shouldn't also create one programmatically, you should create @property (nonatomic, retain) IBOutlet UITableView *tableView;
and connect it to the tableView you configured in IB.
Try to set a breakpoint in the tableView's- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
delegate method to see if this method get called.
From Apple UITableView docs:
A UITableView object must have an object that acts as a data source and an object that acts as a delegate; typically these objects are either the application delegate or, more frequently, a custom UITableViewController object. The data source must adopt the UITableViewDataSource protocol and the delegate must adopt the UITableViewDelegate protocol. The data source provides information that UITableView needs to construct tables and manages the data model when rows of a table are inserted, deleted, or reordered. The delegate provides the cells used by tables and performs other tasks, such as managing accessory views and selections.
As u can see if u don't set a dataSource to your tableView, the tableView will not know how and what to display, so nothing will happen.
You can set one by calling tableView.dataSource = self;
or in IB drag from your tableView to the file's owner (that is your viewController that must implement the UITableViewDataSource
Protocol)
There are two methods in the UITableViewDataSource
protocol that your dataSource must implement:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
If u won't implement those methods u will get a compiler warnings.
You can have more control on how the tableView will look if you implement the UITableViewDelegate
protocol - like row/header/footer height, selections and more...
From Apple UITableView docs:
UITableView overrides the layoutSubviews method of UIView so that it calls reloadData only when you create a new instance of UITableView or when you assign a new data source. Reloading the table view clears current state, including the current selection. However, if you explicitly call reloadData, it clears this state and any subsequent direct or indirect call to layoutSubviews does not trigger a reload.
ReloadData get called when the tableView is created or when you assign a new dataSource (or when you explicitly call it of course..).
This is when the tableView needs to know what to display (how many sections?, how many rows?, and which cell to display?) - So this is when numberOfRowsInSextion
method called.
Recommend
-
87
README.md
-
43
在说控制器瘦身之前,首先要做的的是保证代码结构的清晰化。良好的代码结构有利于代码的传承、可读性以及可维护性。通常笔者都是这样控制代码结构的
-
69
-
57
我们通常会在一个 UIViewController 的 viewDidLoad 方法中通过 self.title = xxx 的方式给一个页面设置其导航栏标题,相信大家对这再熟悉不过了。 如果一个 VC 页面中同时具有
-
53
README.md RouteComposer
-
21
README.md DrawerKit
-
64
对于UIViewController瘦身是一个老生常谈的问题,现在也有比较多的架构来实现此效果,比如MVVM等等。不过这次我们是对于传统的MVC架构设计实现完美的瘦身。此方法也完全不妨碍在此基础上使用MVVM等其他方法瘦身。
-
54
README.md KeyboardAvoiding A SwiftUI view that manages a UIViewController that responds to keyboard events with modified additionalSafeAreaInsets....
-
61
目录 UIViewController 职责 视图管理
-
9
酱油瓶22019.01.30 13:38:52字数 980阅读 187使用 xib 和 storyboard 的优点 开发界面所见即所得,可以快速通过拖拽构造界面。
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK