Subtitle in Table View  iPhone


 

Subtitle in Table View  iPhone

In this tutorial will learn how to write subtitle to the title or how to write the Capital of the State into the same cell of the Table view and index to Table view.

In this tutorial will learn how to write subtitle to the title or how to write the Capital of the State into the same cell of the Table view and index to Table view.

Subtitle in Table View iPhone 

In this tutorial we will discuss how to write subtitle to the title or how to write the Capital of the State into the same cell of the Table view and index to Table view. As explain about the Table view in our previous tutorial, how to create the Table view, how to add or insert the object into the Table View, how to add search bar to Table view and many more using the same concept will add the object into the cell of Table View and added object will be have subtitle to it i.e. State with Capital as its subtitle.In this will also learn how to add index to the Table view. In this will use Navigation Based Application, because will get the table view, there is no need to add table view to the view, and the coding will be done only in RootViewController.h and .m file.

Final View will look like this:

My Project name is SubtitleInTable. This project is created using Navigation Based Application, to insert or to add object into the Table view we have to create an array variable, for two object we will create two array variable in .h file.

RootViewController.h file will look like this:

@interface RootViewController : UITableViewController

{

    NSArray *states;

    NSArray *capitals;

}

@end

Here will add the object into the Table View, to do this will use the array variable which we created in .h file and all this will be written into the viewDidLoad method. Customize the number of rows in the Table View i.e. number of rows in Table View will be set by counting the number of object added by the array into the Table View, And to set the index on the Table View will create an array variable and add object in Table View, the Index will be on right side of the Table View, Index may be in numbers or in alphabets. After setting the index of Table View, now will set the appearance of Table view Cells or Customize the appearance of Table View Cells, this will be done under the cellForRowAtIndexPath method, will take two static NSString and by using that will set the array value to the cell of Table View. After that will set the frame of the cell and also for the data which we have to enter into Table View.

Add this to RootViewController.m file:

- (void)viewDidLoad

{

    [super viewDidLoad];

    states = [[NSArray alloc] initWithObjects:@"Andhra Pradesh",@"Bihar",@"Maharastra",@"West Bangal",@"Rajasthan",@"Punjab",@"Haryana",nil];

    capitals = [[NSArray alloc] initWithObjects:@"Hyderabad",@"Patna",@"Mummbai",@"Colcotta",@"Jaipur",@"Chandigarh", @"Chandigarh",nil];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return [states count];

}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

{

    NSMutableArray *tempArray = [[NSMutableArray alloc] init];

    [tempArray addObject:@"A"];

    [tempArray addObject:@"B"];

    [tempArray addObject:@"C"];

    [tempArray addObject:@"D"];

    [tempArray addObject:@"E"];

    [tempArray addObject:@"F"];

    [tempArray addObject:@"G"];

    [tempArray addObject:@"H"];

    [tempArray addObject:@"I"];

    [tempArray addObject:@"J"];

    [tempArray addObject:@"K"];

    [tempArray addObject:@"L"];

    [tempArray addObject:@"M"];

    [tempArray addObject:@"N"];

    [tempArray addObject:@"O"];

    [tempArray addObject:@"P"];

    [tempArray addObject:@"Q"];

    [tempArray addObject:@"R"];

    [tempArray addObject:@"S"];

    [tempArray addObject:@"T"];

    [tempArray addObject:@"U"];

    [tempArray addObject:@"V"];

    [tempArray addObject:@"W"];

    [tempArray addObject:@"X"];

    [tempArray addObject:@"Y"];

    [tempArray addObject:@"z"];

return tempArray;

}

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

{

    static NSString *CellIdentifier = @"Cell";

    static NSInteger StateTag = 1;

    static NSInteger CapitalTag = 2;

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)

    {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

      CGRect frame;

        frame.origin.x =10;

        frame.origin.y =5;

        frame.size.height =20;

        frame.size.width =200;

        UILabel *stateLabel = [[UILabel alloc] initWithFrame:frame];

        stateLabel.tag = StateTag;

        [cell.contentView addSubview:stateLabel];

        [stateLabel release];

        frame.origin.y += 18;  //this is used to show the another label on to the cell of table

        UILabel *capitalLabel = [[UILabel alloc] initWithFrame:frame];

capitalLabel.tag = CapitalTag;

        [cell.contentView addSubview:capitalLabel];

        [capitalLabel release];

}

    UILabel * stateLabel = (UILabel *) [cell.contentView viewWithTag:StateTag];

    UILabel * capitalLabel = (UILabel *) [cell.contentView viewWithTag:CapitalTag];

    stateLabel.text = [states objectAtIndex:indexPath.row];

    capitalLabel.text = [capitals objectAtIndex:indexPath.row];

    return cell;

}

After making all appropriate corrections into the .h and .m file press Build And Go button

Download Here

Ads