天天看點

data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChecklistItem"];
    return cell;
}
           

These two methods are part of UITableView's data source protocol. The data source is the link between your data and the table view. Usually the view controller plays the role of data source and therefore implements these methods.

The table view needs to know how many rows of data it has and how it should display each of those rows. You should say to the table view: "The view controller is now your data source. You can ask it questions about the data anytime you feel like it."

Once it is hooked up to a data source, the table view sends a numberOfRowsInSection     message when it wants to know how many rows there are. And when the table view needs to display a particular row it sends the CellForRowAtIndexPath  message to ask the data source for a cell.

data source

Don't be confused with cells and rows pp25

When we make numberOfRowsInSection  return the number 5, we tell the table view that there will be five rows. The table view then calls cellForRowAtIndexPath five times, once for each row.

繼續閱讀