-1

I am new to objective-c programming language.I create a table and create all method of table View .But i don't understand about CellForRowAtIndexPath.Please tell me some one how it work.

1 Answers1

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

This is delegate method of UITableView. The returned object is of type UITableViewCell. These are the objects that you see in the table's rows. NSIndexPath has two this Section and Row.

It is called if you implement the UITableViewDataSource protocol in your view controller. A simpler way would be to add a UITableViewController class. I strongly recommend this because it Apple has some code written for you to easily implement the functions that can describe a table. Anyway, if you choose to implement this protocol yourself, you need to create a UITableViewCell object and return it for whatever row. Have a look at its class reference to understand re-usablity because the cells that are displayed in the table view are reused again and again(this is a very efficient design btw).

If your implementing custom cell then I will strongly recommend you use

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

to return only empty cell not set here. use thing like

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

    CartTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCart_Cell" forIndexPath:indexPath];
 return cell;
}

and the use this delegate which will called just after cellForRow data source method

- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{


      if ([cell isKindOfClass:[CartTableViewCell class]])
    {
        CartTableViewCell *cell1 = (CartTableViewCell*)cell;
        MyCartModel* data = [_myCartProductArray objectAtIndex:indexPath.row];
        [cell1 setUpData:data];
    }

}

and set data on UILabel in UITableviewcell custom class.

Sangram Shivankar
  • 3,535
  • 3
  • 26
  • 38
Vinay Kumar
  • 107
  • 1
  • 12