Saturday, June 8, 2013

UITableViewDataSource tableView cellForRowAtIndexPath example in Objective C (iOS).


UITableViewDataSource tableView cellForRowAtIndexPath

Asks the data source for a cell to insert in a particular location of the table view. (required)

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

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

Return Value of [UITableViewDataSource tableView cellForRowAtIndexPath]
An object inheriting from UITableViewCell that the table view can use for the specified row. An assertion is raised if you return nil.

Discussion of [UITableViewDataSource tableView cellForRowAtIndexPath]
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. The identifier for a reusable cell object is assigned when the delegate initializes the cell object by calling the initWithStyle:reuseIdentifier: method of UITableViewCell. 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.

UITableViewDataSource tableView cellForRowAtIndexPath example.
- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CustomCellIdentifier = @"CustomCellIdentifier";

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
    if (cell == nil)
    {
        cell = [[[CustomCell alloc] initByMyWayWithIdentifier:CustomCellIdentifier] autorelease];
    }

    // assign necessary information to cell

    return cell;
}

Example of [UITableViewDataSource tableView cellForRowAtIndexPath].
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    NSInteger section = [indexPath section];

    switch (section) {
        case 0: // First cell in section 1
            cell.textLabel.text = [collectionHelpTitles objectAtIndex:[indexPath row]];
            break;
        case 1: // Second cell in section 1
            cell.textLabel.text = [noteHelpTitles objectAtIndex:[indexPath row]];
            break;
        case 2: // Third cell in section 1
            cell.textLabel.text = [checklistHelpTitles objectAtIndex:[indexPath row]];
            break;
        case 3: // Fourth cell in section 1
            cell.textLabel.text = [photoHelpTitles objectAtIndex:[indexPath row]];
            break;
        default:
            // Do something else here if a cell other than 1,2,3 or 4 is requested
            break;
    }
    return cell;
}

UITableViewDataSource tableView cellForRowAtIndexPath example.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"teacherCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        // More initializations if needed.
    }
    Observations *currentList = [self.teacherNames objectAtIndex:indexPath.row];
    cell.textLabel.text = currentList.obsID;
    return cell;
}

End of UITableViewDataSource tableView cellForRowAtIndexPath example article.