objective-c - UITableViewDataSource中的cellForRowAtIndexPath如何实现?
高洛峰
高洛峰 2017-04-21 10:58:06
0
2
565

在controller中delegate了UITableViewDataSource以后,是必须实现这个方法以展示UITableView的。它的方法原型如下

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

能否结合例子具体解释下NSIndexPath的含义,另外与它想对应的还有一个必须实现的方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

这个section表示的又是什么呢?

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(2)
PHPzhong

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;
洪涛

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 headerheader + rows + footer就构成了一个完整的section. 可以通过- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section; to customize the header, the footer is similar

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!