2017-01-06 4 views
2

私はiOSで新しく、カスタムテーブルビューのセル値からビューコントローラに値を取得する際に問題が発生しています。目的のCのビューコントローラでカスタムTableViewCell値を取得する方法

私のカスタムテーブルビューのセルには、クリックすると4つのボタンがあります。私はcustomtableviewcellのUILabelで値を取得しています。コントローラを表示するためにUILabel値を渡したいと思います。

私のコードは、私は、ボタンの色を変えていると私はUILabelすなわちactuallblでのWebサービスから値を取得しています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"]; 

} 

です。今、私はすべての提案、私はカスタムテーブルビューのセルを使用していると私は、ビューコントローラでPassbtnクリック値をしたい画像のように

enter image description here

ことを行うには、ビューcontroller.Howでその値を取得したいです。私がPassbtnをクリックした場合、私はactuallblの値1を取得していますが、カスタムテーブルビューのセルではどのようにしてこの値をビューコントーラで取得できますか?

ビューController.m事前に

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
     static NSString *[email protected]"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; 
} 

ありがとう!

+0

http://stackoverflow.com/a/41218669/6656894あなたがあなたの考えを持っているこの答えを参照してください –

+0

@HimanshuMoradiya私はすでにボタンを作成しています。ちょうど私はコントローラを表示する値を渡したいと思います。NSUserDefaultを使用していますが、ビューコントローラで呼び出す場所がわかりません。私はテーブルビューのdidselectメソッドでnottingしています。 – Muju

+0

次のコントローラーにデータを渡す必要がありますか?あなたから - (IBAction)wipbtnClick:(id)送信者 {}ボタンをクリック –

答えて

4

ビューコントローラの "cellForRowAtIndexPath"メソッドでセレクタをプログラムで追加できます。これでビューコントローラメソッドを呼び出すことができ、このメソッドは送信者をパラメータとして与え、送信者のスーパービューからセルを取得できます。これで、セルのセルボタンがタップされ、セルからuilabelテキストの価値を得ることができます。

-(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); 
} 
+0

UITableViewCellContentView actuallbl]:インスタンスに送信された認識できないセレクタがエラーを表示しています。 – Muju

+0

この行のUIViewでAudittableviewcellを分析します。CartTableViewCell * cell = sender.superview – Muju

+0

CartTableViewCell * cell =(CartTableViewCell *)sender.superview.superview – aafat

-1

まずここでは、セルのindexValueを取得し、yourArray

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

} 

からラベル値を取得しますcellForRowAtindexPath

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

にAuditTableViewCellに、ボタンの出口を作り、 ボタンのaddTargetを作り、 myActionからコントローラを表示し、値をビューコントローラに渡すことができます

関連する問題