1

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
Vinodh
  • 5,262
  • 4
  • 38
  • 68
  • Try to debug by using NSLog and print the cell index and match with the table or row .. To full fill your condition.. and then you can find solution ... Because in question .. I can't fully understand your conditions. – Abhishek Mishra Oct 03 '17 at 10:39
  • how to add disclosure indicator to first table view ==> go to storyboard --> select your cell --> Accessory --> select disclosure indicator – Nirav Kotecha Oct 03 '17 at 10:47
  • I got solution for my first question. Only one thing left and that is Disclosure Indicator. @Nirav as per your answer I followed but the thing is that I was taken tableview and not tableview cell. Sp how can I add that disclosure indicator ? is there any way to add programmatically ? – Shruti kadam Oct 03 '17 at 12:02
  • try this.. `cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;` – Nirav Kotecha Oct 03 '17 at 12:47

1 Answers1

2

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];

}