在controller中delegate了UITableViewDataSource
以后,是必须实现这个方法以展示UITableView的。它的方法原型如下
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
能否结合例子具体解释下NSIndexPath
的含义,另外与它想对应的还有一个必须实现的方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
这个section表示的又是什么呢?
PHPz2017-04-21 11:00:06
First explain the layout of TableView. The content of a TableView is two-level, Section and Row in Section. The view corresponding to each Row becomes TableViewCell. Take settings.app as an example, as shown below:
There are two Sections in the picture
There are 6 Rows in Section 0 -- Airplane Mode~Operator
You can see 3 Rows in Section 1 - Sound~Desktop
Let’s talk about the two methods in the question.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
This method returns a certain cell, which can be rendered in various customized styles or rendered in several default styles.
IndexPath marks the location of the cell you want to render. There are two attributes in the indexPath object, section and row. As the name suggests, a certain cell can be located.
Note: This method will be called when a certain cell appears on the screen, and it will be called once if it does not appear once. Because a cell object is recycled when the visible area of the screen disappears, and is reused for other cells that appear in the visible area. Therefore, when rendering a cell in this method, it is best to use a for loop or something to clean the cell.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
This method is called when the TableView is generated (or when reloadData is called on the tableView object) and returns the number of rows (cells) in a certain section.
For example, in the settings.app interface, it should be
switch (seciton) { 0 : return 6; 1 : return 3; } return 0;
PHP中文网2017-04-21 11:00:06
1, NSIndexPath
主要包含(NSUInteger)section
和(NSUInteger)row
,每个row
必然是属于一个section
的,否则这个row
没有意义,- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
里面就是表明各section
有多少row
.
2. Open Contacts
,里面的那些A、B、C...这样的标题就属于section header
,header + rows + footer
就构成了一个完整的section
. 可以通过- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
to customize the header, the footer is similar