Search code examples
iosobjective-ciphoneuitableviewuitableviewrowaction

How to give action to tableview row and how to add Add disclosure indicator in it?


Here is description of my class.

This is screenshots of first tableview

At first refer the above image.

So here, I created a button.(see in scene 1 in Image) After clicking a button one table view will appear (see in scene 2 in Image). If I click any cell then that table view cell value display on that button text (see in scene 3 in Image). Here I not given any action to any table view cell.

Now, I given action to these row(only to index 0 for testing). When I click uitableviewcell on Uitableview 1 then it will show another tableview 2 and previous table view hide. see following image.

Second Table view

So in this tableview 2 I want to give action i.e when i click on any row from tableview row of second table for example :Suppose I clicked on Arjun then button label need to change by Arjun and this tableview have to hide or If click on tableview row like Karan then on one pop up I want to display like Karan selected.

In short, I want to know how to give action to that second tableview row and how to add disclosure indicator to first table view.

ViewCotroller.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UIButton *button;
@property (weak, nonatomic) IBOutlet UITableView *tableview1;
@property (weak, nonatomic) IBOutlet UITableView *tableview2;

@property(strong,nonatomic)NSArray *arr1;
@property(strong,nonatomic)NSArray *arr2;

- (IBAction)buttonAction:(id)sender;

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.arr1=[[NSArray alloc]initWithObjects:@"One",@"Two",@"Three",@"Four",@"Five", nil];
     self.arr2=[[NSArray alloc]initWithObjects:@"Arjun",@"Karan",@"Amar", nil];

    self.tableview1.delegate=self;
    self.tableview1.dataSource=self;
    self.tableview2.delegate=self;
    self.tableview2.dataSource=self;

     self.tableview1.hidden=YES;
    self.tableview2.hidden=YES;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger rows;

    if(tableView == _tableview1) rows = [_arr1 count];
    if(tableView == _tableview2) rows = [_arr2 count];

    return rows;
}

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

    static  NSString *simple=@"SampleIndentifier";

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:simple];

    if(cell==nil)
    {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simple];
     }

   // cell.textLabel.text=[self.arr1 objectAtIndex:indexPath.row];

    if(tableView == _tableview1)
        cell.textLabel.text = [self.arr1 objectAtIndex:indexPath.row];


    if(tableView == _tableview2)
        cell.textLabel.text = [self.arr2 objectAtIndex:indexPath.row];

    return  cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *cell= [self.tableview1 cellForRowAtIndexPath:indexPath];

    [self.button setTitle:cell.textLabel.text forState:UIControlStateNormal];
    self.tableview1.hidden=YES;


    if(tableView == _tableview1)
    {
        if(indexPath.row==0)
        {
            self.tableview1.hidden=YES;
            self.tableview2.hidden=NO;
                if(indexPath.row==0)
                {
                // cell.textLabel.text = [self.arr2 objectAtIndex:indexPath.row];

                }
          //  cell.textLabel.text = [self.arr2 objectAtIndex:indexPath.row];

        }

        //cell.textLabel.text = [self.arr1 objectAtIndex:indexPath.row];
     //self.tableview2.hidden=YES;
    }
    if(tableView == _tableview2)
        cell.textLabel.text = [self.arr2 objectAtIndex:indexPath.row];

}

- (IBAction)buttonAction:(id)sender {

    if(self.tableview1.hidden==YES)
    {
        self.tableview1.hidden=NO;
    }
    else
    {
         self.tableview1.hidden=YES;
    }
}
@end

Solution

  • check following answer..!

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
        UITableViewCell *cell= [self.tableview1 cellForRowAtIndexPath:indexPath];
    
        [self.button setTitle:cell.textLabel.text forState:UIControlStateNormal];
        self.tableview1.hidden=YES;
    
    
        if(tableView == _tableview1)
        {
            if(indexPath.row==0)
            {
                self.tableview1.hidden=YES;
                self.tableview2.hidden=NO;
                    if(indexPath.row==0)
                    {
                    // cell.textLabel.text = [self.arr2 objectAtIndex:indexPath.row];
    
                    }
              //  cell.textLabel.text = [self.arr2 objectAtIndex:indexPath.row];
    
            }
    
            //cell.textLabel.text = [self.arr1 objectAtIndex:indexPath.row];
         //self.tableview2.hidden=YES;
        }
        if(tableView == _tableview2)
            cell.textLabel.text = [self.arr2 objectAtIndex:indexPath.row];
    
    }