blog

Home / DeveloperSection / Blogs / Displaying Contents into UITableView Cell With Its Explanation

Displaying Contents into UITableView Cell With Its Explanation

Tarun Kumar2607 17-Nov-2015

Whenever we use UITableView in our iOS app and want to display contents into table view cell, we need to follow some necessary steps in correct way. Whenever we display contents into table view cell, our delegate sent a message of tableView:cellForRowAtIndexPath.

Here, we provide the code with its explanation, use this method in ViewController.m file:

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

{

     static NSString *TableCell = @"Cell";

     UITableViewCell *cell = [tableView  dequeueReusableCellWithIdentifier:TableCell];

     // UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1

reuseIdentifier:TableCell];

 

     NSDictionary * data = self.destinationsArray [indexPath.row];

     NSAttributedString *attributedString = [[NSAttributedString alloc]

initWithString: data[@"Destination Name"]

attributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:17.0f],                NSForegroundColorAttributeName: [UIColor whiteColor]}];

     cell.textLabel.attributedText = attributedString;

     return cell;

}

 First of all we determine whether any cells that we can use are lying around. As we know that a TableView can display quite a few rows at a time on the iPhone or iPad screen, the table itself can conceivably hold a lot more. 

  A large table will take a lot of memory, if we create cells for every row. So, UITableView solve this issue with cell reuseability, tableView cells scroll off the screen, they’re placed in a queue of cells available to be reused. 

     If the system runs low on memory, the Table view gets rid of the cells in the queue, but as long as it has some available memory for them, it holds on to them in case we want to use them again. 

  We will create a NSString to use as a cell identifier, which indicates what cell type we are using:

static NSString *TableCell = @"Cell"; 

    Table views also supports multiple cell types that makes identifier necessary. In our code, we use only one cell type, but sometimes we need to create more than one cell identifiers. 

  We will ask the TableView for a specific reusable cell object by sending it a dequeueReusableCellWithIdentifier: message:

UITableViewCell *cell = [tableView   dequeueReusableCellWithIdentifier:TableCell];

It will create a cell using the cell identifier that we recently specified, now we have a TableView cell that we will return to the Table view. 

TableView provide different formats for tableView cell, here are other
formats that we can use in our code: 


UITableViewCellStyleDefault: it gives us a simple cell with a Text label (black and left-aligned) and an optional Image view. [Basic]

UITableViewCellStyleValue1: it gives us a cell with a left-aligned black Text label on the left side of the cell and a right-aligned Text label with smaller gray text on the right side. (The Settings app uses this style of cell.) [Right Detail]

UITableViewCellStyleValue2: it gives us a cell with a right-aligned blue Text label on the left side of the cell and a left-aligned black Text label on the right side of the cell. [Left Detail]

UITableViewCellStyleSubtitle: it gives us a cell with a left-aligned Text label across the top and a left-aligned Text label below it in smaller gray text. [Subtitle]

     With the formatting out of the way, we then set the Label properties that we are interested to display in cell. 

      We pluck out the name for each destination we have stored by accessing the DestinationName in each Destination dictionary. We do that by accessing the dictionary in the (saved) destinationsArray corresponding to the sections and row in indexPath, which contains the section and row information in a single object. 

     To get the row or the section out of an NSIndexPath, we just have to invoke its section method (indexPath.section) or its row method (indexPath.row), either of which returns an int:

NSDictionary * data =   destinationsArray[indexPath.row]; 

     now, create an attributedString, which can manage both the character strings and attributes like- fonts, colors, and even kerning:

NSAttributedString *attributedString = [[NSAttributedString alloc]   initWithString:data[@"Destination​Name"]   attributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:17.0f],   NSForegroundColorAttributeName: [UIColor whiteColor]}]; 

After, to format cell text label, use this code (it will set the text label into cell):

cell.textLabel.attributedText = attributedString; 

  At the end, we return the formatted cell, that will responsible to display text in that row:

return cell;


Updated 13-Mar-2018

Leave Comment

Comments

Liked By