How to set up UITableView within a UIViewController created on a .xib file

前端 未结 3 811
一个人的身影
一个人的身影 2021-01-31 21:17

I have a class like this:

@interface ExerciseLogDetails : UIViewController {
         


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-31 21:42

    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  
    

    and

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

提交回复
热议问题