天天看點

做一個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.