Muju
Muju

Reputation: 979

How to get custom TableViewCell value on view controller in objective c

I am new in iOS and I am facing problem regarding to get the value from custom table view cell value to the view controller.

In my custom table view cell there are 4 button on the click I am getting value in UILabel of customtableviewcell I want to pass UILabel value to view controller.

My code is like this in customtableviewcell.m

- (void)awakeFromNib {


    passbtn.layer.cornerRadius = passbtn.bounds.size.width / 2.0;// this value vary as per your desire
    passbtn.clipsToBounds = YES;

    failbtn.layer.cornerRadius = failbtn.bounds.size.width / 2.0;// this value vary as per your desire
    failbtn.clipsToBounds = YES;

    wipbtn.layer.cornerRadius = wipbtn.bounds.size.width / 2.0;// this value vary as per your desire
    wipbtn.clipsToBounds = YES;

    nabtn.layer.cornerRadius = nabtn.bounds.size.width / 2.0;// this value vary as per your desire
    nabtn.clipsToBounds = YES;


    infobtn.layer.cornerRadius = infobtn.bounds.size.width / 2.0;// this value vary as per your desire
    [[infobtn layer] setBorderWidth:1.0f];
    infobtn.layer.borderColor =[[UIColor blueColor] CGColor];
    infobtn.clipsToBounds = YES;

    passlbl.hidden=YES;
    faillbl.hidden=YES;
    warninglbl.hidden=YES;
    nalbl.hidden=YES;
    actuallbl.hidden=YES;

    // Initialization code
}
- (void)textViewDidEndEditing:(UITextView *)theTextView
{
    if (![textView1 hasText]) {
        lbl.hidden = NO;
    }
}

- (void) textViewDidChange:(UITextView *)textView
{
    if(![textView hasText]) {
        lbl.hidden = NO;
    }
    else{
        lbl.hidden = YES;
    }  
}


-(IBAction)passbtnClick:(id)sender
{
    passbtn.backgroundColor=[UIColor greenColor];
    failbtn.backgroundColor=[UIColor lightGrayColor];
    wipbtn.backgroundColor=[UIColor lightGrayColor];
    nabtn.backgroundColor=[UIColor lightGrayColor];

    actuallbl.text=passlbl.text;
    ActualString=actuallbl.text;
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    [defaults setObject:ActualString forKey:@"ActualStringCustom"];

}
-(IBAction)failbtnClick:(id)sender
{
    passbtn.backgroundColor=[UIColor lightGrayColor];
    failbtn.backgroundColor=[UIColor redColor];
    wipbtn.backgroundColor=[UIColor lightGrayColor];
    nabtn.backgroundColor=[UIColor lightGrayColor];

    actuallbl.text=faillbl.text;
    ActualString=actuallbl.text;
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    [defaults setObject:ActualString forKey:@"ActualStringCustom"];

    UIAlertView *testAlert = [[UIAlertView alloc] initWithTitle:@"Fail!"
                                                        message:audittitlelbl.text
                                                       delegate:self
                                              cancelButtonTitle:nil
                                              otherButtonTitles:@"Done", nil];
    textView1 = [UITextView new];
    lbl = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 0.0,90.0, 34.0)];


    [lbl setText:@"Enter Remark"];
    [lbl setFont:[UIFont systemFontOfSize:12]];
    [lbl setBackgroundColor:[UIColor clearColor]];
    [lbl setTextColor:[UIColor lightGrayColor]];
    textView1.delegate = self;

    [textView1 addSubview:lbl];

    [testAlert setValue: textView1 forKey:@"accessoryView"];

    [testAlert show];
}
-(IBAction)wipbtnClick:(id)sender
{
    passbtn.backgroundColor=[UIColor lightGrayColor];
    failbtn.backgroundColor=[UIColor lightGrayColor];
    wipbtn.backgroundColor=[UIColor orangeColor];
    nabtn.backgroundColor=[UIColor lightGrayColor];

    actuallbl.text=warninglbl.text;
    ActualString=actuallbl.text;
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    [defaults setObject:ActualString forKey:@"ActualStringCustom"];

    UIAlertView *testAlert = [[UIAlertView alloc] initWithTitle:@"Warning!"
                                                        message:audittitlelbl.text
                                                       delegate:self
                                              cancelButtonTitle:nil
                                              otherButtonTitles:@"Done", nil];
    textView1 = [UITextView new];
    lbl = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 0.0,90.0, 34.0)];


    [lbl setText:@"Enter Remark"];
    [lbl setFont:[UIFont systemFontOfSize:12]];
    [lbl setBackgroundColor:[UIColor clearColor]];
    [lbl setTextColor:[UIColor lightGrayColor]];
    textView1.delegate = self;

    [textView1 addSubview:lbl];

    [testAlert setValue: textView1 forKey:@"accessoryView"];

    [testAlert show];
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {

    if(buttonIndex==0)
    {

    }
}
-(IBAction)nabtnClick:(id)sender
{
    passbtn.backgroundColor=[UIColor lightGrayColor];
    failbtn.backgroundColor=[UIColor lightGrayColor];
    wipbtn.backgroundColor=[UIColor lightGrayColor];
    nabtn.backgroundColor=[UIColor blueColor];
    actuallbl.text=nalbl.text;
    ActualString=actuallbl.text;
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    [defaults setObject:ActualString forKey:@"ActualStringCustom"];

}

In this code I am changing colour of button and I am getting value from web service in UILabel i.e actuallbl. Now I want to get that value in the view controller.How to do that any suggestion

enter image description here

As in the image I am using custom table view cell And I want Passbtn click value in the view controller. If I click on Passbtn I am getting value 1 in actuallbl but in custom table view cell so how can I get this value in view contoller.

View Controller.m

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
         static NSString *STI=@"STI";
         AuditTableViewCell *cell = (AuditTableViewCell *)[tableView dequeueReusableHeaderFooterViewWithIdentifier:STI];
         if (cell == nil)
         {
                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"AuditTableViewCell" owner:self options:nil];
                cell = [nib objectAtIndex:0];
                cell.accessoryType=UITableViewCellAccessoryNone;
         }
         cell.audittitlelbl.text=[NSString stringWithFormat:@"%@",[idarray objectAtIndex:indexPath.row]];
         cell.passlbl.text=[NSString stringWithFormat:@"%@",[Passarray objectAtIndex:indexPath.row]];
         cell.faillbl.text=[NSString stringWithFormat:@"%@",[Failarray objectAtIndex:indexPath.row]];
         cell.warninglbl.text=[NSString stringWithFormat:@"%@",[Warningarray objectAtIndex:indexPath.row]];
         cell.nalbl.text=[NSString stringWithFormat:@"%@",[NAarray objectAtIndex:indexPath.row]];
         return cell;
}

Thanks in Advance!

Upvotes: 2

Views: 1981

Answers (2)

Amit Verma
Amit Verma

Reputation: 1413

First make outlet of button to AuditTableViewCell and Make addTarget of button in cellForRowAtindexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        static NSString *STI=@"STI";
        AuditTableViewCell *cell = (AuditTableViewCell *)[tableView dequeueReusableHeaderFooterViewWithIdentifier:STI];
        [cell.passbtn addTarget:self  action:@selector(myAction:)forControlEvents:UIControlEventTouchUpInside];
        cell.passbtn.tag = indexPath.Row;
} 

Here you will get indexValue of Cell and fetch label Value from yourArray

-(void)myAction:(UIButton *)sender 
 {
  NSlog(@"%f",sender.tag);
  NSlog(@"%@",[Passarray objectAtIndex:sender.tag]);

 }

And you can push to view controller from myAction and pass value to view controller

Upvotes: -1

aafat
aafat

Reputation: 148

You can add selector programmatically in view controller "cellForRowAtIndexPath" method . Now you can call view controller method and this method give sender as a parameter then you can get cell from superview of sender . Now you have cell that cell button tapped and you can get value of uilabel text from cell.

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    CartTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

    CartModel *model = [arrCartData objectAtIndex:indexPath.row];

    cell.lblCartBrandName.text=model.strBrandName;

    [cell.btnQuantity addTarget:self action:@selector(btnQuantityPressed:) forControlEvents:UIControlEventTouchUpInside];


    return cell;

}


-(void)btnQuantityPressed:(UIButton *)sender 
{
      CartTableViewCell *cell = sender.superview.superview;
    NSLog("%@",cell.lblCartBrandName.text);
}

Upvotes: 5

Related Questions