Akhil Jiji
Akhil Jiji

Reputation: 185

Handling Individual cell Selection and its action in UITableView

I want to select each cell in a UITableView and to perform an action associated with each cell.

For example if I select first Cell in a row,it should direct me to one page and if I select the second Cell , it should direct me to some other page.How is it possible?Can someone provide me a sample code?

Upvotes: 1

Views: 2060

Answers (5)

Ajay Kumar
Ajay Kumar

Reputation: 1827

You can do those stuff in didSelectRowAtIndexPath -

In objectiveC -

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   if(indexPath.row == 0) // first cell 
   {
    // Do your stuff for 1st cell selection;
   }
  else if(indexPath.row == 1)// second Cell
   {
    // Do your stuff for 2nd cell selection
   }
   //continue like this
}

In Swift -

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}

Once just go through this link https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TableView_iPhone/CreateConfigureTableView/CreateConfigureTableView.html

Upvotes: 1

Aamir
Aamir

Reputation: 17007

Try following code, you can use method of UITableView to achieve this:

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 0) // first cell
        NSLog(@"Perform action of first cell selection");
    else if(indexPath.row == 1) // second cell
        NSLog(@"Perform action of second cell selection");
    else if(indexPath.row == 2) // third cell
        NSLog(@"Perform action of third cell selection");
    else if(indexPath.row == 3) // fourth cell
        NSLog(@"Perform action of fourth cell selection");
    else if(indexPath.row == 4) // fifth cell
        NSLog(@"Perform action of fifth cell selection");
    else 
        NSLog(@"Perform action for all rows except first 5 rows");

    return indexPath;
}

Upvotes: 2

mcfisty
mcfisty

Reputation: 207

The other answers are fundamentally correct, but in case you want a Swift solution:

    performSegueWithIdentifier(identifier: segueIDs[indexPath.row], sender: self)

Here segueIDs should be defined as an array of strings containing all the segue identifiers that you need to get to the other views, in the same order as the table cells will be.

As an example, if

    var segueIDs = ["first", "second", "third", "fourth", "fifth"]

then pressing the middle cell would take you to the screen that "third" links to. Happy coding.

Upvotes: 0

SeraZheng
SeraZheng

Reputation: 197

This sample code may help you.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger index = indexPath.row;

    switch (index) {
        case 0:
            // Initilal your first page and redirect to that

              break;
        case 1:
            // Initilal your second page and redirect to that

            break;
        case 2:
            // Initilal your three page and redirect to that

            break;
        case 3:
            // Initilal your four page and redirect to that

            break;
        case 4:
            // Initilal your five page and redirect to that

            break;
        default:
            //do your default

            break;
    }
}

Upvotes: 0

Enrico Susatyo
Enrico Susatyo

Reputation: 19800

You need to hook into the table view's delegate methods and do the action in the - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath method.

Go read Apple's guide on Table View.

Upvotes: 0

Related Questions