• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 如何在 objective-c 中的 View Controller 上获取自定义 TableViewCell 值

[复制链接]
菜鸟教程小白 发表于 2022-12-12 12:43:47 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我是 iOS 新手,我遇到了从自定义表格 View 单元格值获取值到 View Controller 的问题。

在我的自定义表格 View 单元格中,单击时有 4 个按钮我在 customtableviewcell 的 UILabel 中获取值我想将 UILabel 值传递给 View Controller 。

我的代码在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)textViewDidEndEditingUITextView *)theTextView
{
    if (![textView1 hasText]) {
        lbl.hidden = NO;
    }
}

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


-(IBAction)passbtnClickid)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)failbtnClickid)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)wipbtnClickid)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)alertViewUIAlertView *)alertView didDismissWithButtonIndexNSInteger)buttonIndex {

    if(buttonIndex==0)
    {

    }
}
-(IBAction)nabtnClickid)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"];

}

在这段代码中,我正在更改按钮的颜色,并且我正在从 UILabel 中的 Web 服务中获得值(value),即实际。现在我想在 View Controller 中获取该值。如何执行任何建议

enter image description here

如图所示,我正在使用自定义表格 View 单元格,并且我希望在 View Controller 中使用 Passbtn 单击值。如果我点击 Passbtn 我在实际中得到值 1 但在自定义表格 View 单元格中,那么我怎样才能在 View Controller 中获得这个值。

查看 Controller .m

 - (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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;
}

提前致谢!



Best Answer-推荐答案


您可以在 View Controller “cellForRowAtIndexPath”方法中以编程方式添加选择器。现在您可以调用 View Controller 方法,并且此方法将 sender 作为参数,然后您可以从 sender 的 superview 获取单元格。现在你有了点击了单元格按钮的单元格,你可以从单元格中获取 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);
}

关于ios - 如何在 objective-c 中的 View Controller 上获取自定义 TableViewCell 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41499128/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap