Abdullah Nurum Abdullah Nurum - 1 month ago 9
Objective-C Question

Do some thing when press UITableViewCell

I created table view in

UIViewController
, and successfully created cells, but I could not figure out how to do something when press any cell. I want if press one cell, go to another view controller. I added some methods but did not work. What methods should I add and how to connect the method with action?

This method is for create Array

-(void)addCell{

int wholeNumber = 500;
int leftOver = wholeNumber%90;
int sectionNumber = wholeNumber / 90;;

tableTitle = [[NSMutableArray alloc] init];
tableSubtitle = [[NSMutableArray alloc] init];

for (int i =1; i<= sectionNumber; i++) {
if (i == 1) {
NSString *stringForTitle = [NSString stringWithFormat:@"%d",i];
NSString *stringForsubitle = [NSString stringWithFormat:@"90 Questions. Free."];
[tableTitle addObject:stringForTitle];
[tableSubtitle addObject:stringForsubitle];


}else if (i==2){
NSString *stringForTitle = [NSString stringWithFormat:@"%d",i];

NSString *stringForsubitle = [NSString stringWithFormat:@"90 Questions. Unlock or Rate App."];
[tableTitle addObject:stringForTitle];
[tableSubtitle addObject:stringForsubitle];
}else if (i==3){
NSString *stringForTitle = [NSString stringWithFormat:@"%d",i];

NSString *stringForsubitle = [NSString stringWithFormat:@"90 Questions. Unlock or Sign in and rate 15 questions."];
[tableTitle addObject:stringForTitle];
[tableSubtitle addObject:stringForsubitle];
}else if (i==4){
NSString *stringForTitle = [NSString stringWithFormat:@"%d",i];

NSString *stringForsubitle = [NSString stringWithFormat:@"90 Questions. Unlock or Sign in and write explanation for 4 questions."];
[tableTitle addObject:stringForTitle];
[tableSubtitle addObject:stringForsubitle];
}else if (i>4){
NSString *stringForTitle = [NSString stringWithFormat:@"%d",i];

NSString *stringForsubitle = [NSString stringWithFormat:@"90 Questions. Unlock or Sign in and contribute 2 questions."];
[tableTitle addObject:stringForTitle];
[tableSubtitle addObject:stringForsubitle];
}else{

}




}
if (leftOver == 0) {


} else {
if (sectionNumber == 0) {
NSString *stringForTitle = [NSString stringWithFormat:@"1"];

NSString *stringForsubitle = [NSString stringWithFormat:@"%d Questions. Free.",leftOver];
[tableTitle addObject:stringForTitle];
[tableSubtitle addObject:stringForsubitle];
}else if (sectionNumber==1){
NSString *stringForTitle = [NSString stringWithFormat:@"2"];

NSString *stringForsubitle = [NSString stringWithFormat:@"%d Questions. Unlock or Rate app.",leftOver];
[tableTitle addObject:stringForTitle];
[tableSubtitle addObject:stringForsubitle];
}else if (sectionNumber==2){
NSString *stringForTitle = [NSString stringWithFormat:@"3"];

NSString *stringForsubitle = [NSString stringWithFormat:@"%d Questions. Unlock or Sign in and rate 15 questions.",leftOver];
[tableTitle addObject:stringForTitle];
[tableSubtitle addObject:stringForsubitle];
}else if (sectionNumber==3){
NSString *stringForTitle = [NSString stringWithFormat:@"4"];

NSString *stringForsubitle = [NSString stringWithFormat:@"%d Questions. Unlock or Sign in and write explanation for 3 questions.",leftOver];
[tableTitle addObject:stringForTitle];
[tableSubtitle addObject:stringForsubitle];
}else if (sectionNumber>3){
NSString *stringForTitle = [NSString stringWithFormat:@"%d",sectionNumber +1];

NSString *stringForsubitle = [NSString stringWithFormat:@"%d Questions. Unlock or Sign in and contribute 1 questions.",leftOver];
[tableTitle addObject:stringForTitle];
[tableSubtitle addObject:stringForsubitle];
}else {

}

}

NSString *stringForTitle = [NSString stringWithFormat:@"CQ"];
NSString *stringForsubitle = [NSString stringWithFormat:@"Contribute Questions."];
[tableTitle addObject:stringForTitle];
[tableSubtitle addObject:stringForsubitle];




}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableTitle count];
}


This method Creates Cell

-(UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"thisCell"];



UILabel *titleLable= [[UILabel alloc] initWithFrame:CGRectMake(0, 4, 60, 40)];
titleLable.text = [tableTitle objectAtIndex:indexPath.row];

[titleLable setFont:[UIFont systemFontOfSize:25]];
titleLable.backgroundColor = [UIColor whiteColor];
titleLable.textAlignment = NSTextAlignmentCenter;
CALayer* layer = [titleLable layer];
CALayer *bottomBorder = [CALayer layer];
bottomBorder.borderColor = [UIColor lightGrayColor].CGColor;
bottomBorder.borderWidth = 1;
bottomBorder.frame = CGRectMake(-1, layer.frame.size.height-1, layer.frame.size.width, 1);
[bottomBorder setBorderColor:[UIColor lightGrayColor].CGColor];
[layer addSublayer:bottomBorder];

[cell.contentView addSubview:titleLable];
CGFloat width = [UIScreen mainScreen].bounds.size.width;
CGFloat actualwidth = width - 100;


UILabel *subtitleLable = [[UILabel alloc] initWithFrame:CGRectMake(60, 4, actualwidth, 40)];
subtitleLable.text = [tableSubtitle objectAtIndex:indexPath.row];
subtitleLable.numberOfLines = 0;

[subtitleLable setFont:[UIFont systemFontOfSize:10]];
subtitleLable.backgroundColor = [UIColor whiteColor];


CALayer* layer2 = [subtitleLable layer];
CALayer *bottomBorder2 = [CALayer layer];
bottomBorder2.borderColor = [UIColor lightGrayColor].CGColor;
bottomBorder2.borderWidth = 1;
bottomBorder2.frame = CGRectMake(-1, layer2.frame.size.height-1, layer2.frame.size.width, 1);
[bottomBorder2 setBorderColor:[UIColor lightGrayColor].CGColor];
[layer2 addSublayer:bottomBorder2];



[cell.contentView addSubview:subtitleLable];


UILabel *spaceLable = [[UILabel alloc] initWithFrame:CGRectMake(actualwidth+60, 4, 100, 40)];

spaceLable.text = @"";
spaceLable.numberOfLines = 0;

[spaceLable setFont:[UIFont systemFontOfSize:11]];
spaceLable.backgroundColor = [UIColor whiteColor];


CALayer* layer3 = [spaceLable layer];
CALayer *bottomBorder3 = [CALayer layer];
bottomBorder3.borderColor = [UIColor lightGrayColor].CGColor;
bottomBorder3.borderWidth = 1;
bottomBorder3.frame = CGRectMake(-1, layer3.frame.size.height-1, layer3.frame.size.width, 1);
[bottomBorder3 setBorderColor:[UIColor lightGrayColor].CGColor];
[layer3 addSublayer:bottomBorder3];


[cell.contentView addSubview:spaceLable];












return cell;
}


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


This is image form Table View

enter image description here

here is table view delegate connection image.enter image description here

Answer

There are some option to do it.

  1. You already have delegate of tableview itself. Set the delegate like :

    self.tableView.delegate = self;
    

Now implement your delegate when user select any cell.

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

   int ClickedRow = indexPath.row;
  // You can go further with this ID->ClickedRow 

 }
  1. You can add UIButton to tablecell.

     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
     {
    
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"thisCell"];
    
    
      UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    
      //set the position of the button
     button.frame = CGRectMake(cell.frame.origin.x + 100, cell.frame.origin.y + 20, 100, 30);
     [button setTitle:@"myButton" forState:UIControlStateNormal];   
      [button addTarget:self action:@selector(customActionPressed:) forControlEvents:UIControlEventTouchUpInside];
      button.backgroundColor= [UIColor clearColor];
      [cell addSubview:button];
    
     return cell;
     }
    
    
      -(void)customActionPressed:(UIButton*)sender
      {
    
       NSLog(@"%i",[sender tag]); / with this tag, you can do anything..
    
      }
    
  2. You can add gesture: Inside cellForRowAtIndexPath , do it:

         UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]    initWithTarget:self action:@selector(handleImageTap:)];
        tap.cancelsTouchesInView = YES;
        tap.numberOfTapsRequired = 1;
        [imageView addGestureRecognizer:tap];
    
    
       // handle method
     - (void) handleImageTap:(UIGestureRecognizer *)gestureRecognizer 
      {
         //Do anything here ....
       }