做一个TableView 的iOS App的笔记(二)

有一些代码修改,一些假装读懂了,一些存疑。

objectivec
-(void)configureCheckmarkForCell :(UITableViewCell *)cell withChecklistItem:(CheckListItem *)item{ if (item.checked) { cell.accessoryType = UITableViewCellAccessoryCheckmark; } else { cell.accessoryType = UITableViewCellAccessoryNone; } }

这一个的作用是根据ChecklistItem来让cell呈现它的Checkmark的状态,要么勾选,要么不勾选。
所以两个参数,cell和item。

objectivec-(void)configureTextForCell: (UITableViewCell *)cell
          withChecklistItem:(CheckListItem *)item{

    UILabel *label = (UILabel *)[cell viewWithTag:1000];
    label.text = item.text;

}

同上一个函数类似,根据ChecklistItem来让cell中的label的文字变化。
所以两个参数,cell和item。

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChecklistItem"];

    CheckListItem *item = _items[indexPath.row];

    [self configureTextForCell:cell withChecklistItem:item];
    [self configureCheckmarkForCell:cell withChecklistItem:item];

    return cell;
}

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

所以理解下来就是一直问data source要cell的,老要之前那个prototype cell(通过id知道的),然后再用函数吧item取出来,cell也有了,item也有了,在通过之前的函数把cell装载好,return出去。

Description
Asks the data source for a cell to insert in a particular location of the table view. (required)
The returned UITableViewCell object is frequently one that the application reuses for performance reasons. You should fetch a previously created cell object that is marked for reuse by sending a dequeueReusableCellWithIdentifier: message to tableView. Various attributes of a table cell are set automatically based on whether the cell is a separator and on information the data source provides, such as for accessory views and editing controls.

Parameters
tableView
A table-view object requesting the cell.
indexPath
An index path locating a row in tableView.

Returns
An object inheriting from UITableViewCell that the table view can use for the specified row. An assertion is raised if you return nil.

objectivec- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    CheckListItem *item = _items[indexPath.row];

    [item toggleChecked];
    [self configureCheckmarkForCell:cell withChecklistItem:item];

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

  • (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

这个对于我来说好像存疑更多一点,告诉我们现在哪个row被选中了,得到cell和item,但是是通过哪个cell被选中了来得到cell和item的.

假装比较妙的一点是在它用来toggleCheck的一点,就是把这件事真心交给item的method去做了,这样数据就是与row相关,与数据相关,而非cell相关,然后再次调用函数用来重新configure cell的checkmark.

当然,依旧存疑。

Description
Tells the delegate that the specified row is now selected.
The delegate handles selections in this method. One of the things it can do is exclusively assign the check-mark image (UITableViewCellAccessoryCheckmark) to one row in a section (radio-list style). This method isn’t called when the editing property of the table is set to YES (that is, the table view is in editing mode). See “€œ“Managing Selections”” in Table View Programming Guide for iOS for further information (and code examples) related to this method.

Parameters
tableView
A table-view object informing the delegate about the new row selection.
indexPath
An index path locating the new selected row in tableView.

    原文作者:yuxue
    原文地址: https://segmentfault.com/a/1190000003043661
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞