我是iOS的新手,我遇到了从自定义表格视图单元格值到视图控制器的问题。
在我的自定义表视图单元格中,点击了4个按钮我在customtableviewcell的UILabel中获取值我想将UILabel值传递给视图控制器。
我的代码在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"];
}
在这段代码中,我正在改变按钮的颜色,我从UILabel的web服务中获得价值,即actuallbl。现在我想在视图控制器中获取该值。如何做任何建议
在图像中我正在使用自定义表格视图单元格我想在视图控制器中单击Passbtn值。如果我点击Passbtn我在actuallbl中获得值1但在自定义表格视图单元格中,那么如何在视图控制器中获取此值。
查看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;
}
提前致谢!
答案 0 :(得分: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);
}
答案 1 :(得分:-1)
首先将按钮的出口发送到AuditTableViewCell和 在cellForRowAtindexPath
中创建按钮的addTarget- (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;
}
在这里,您将获得Cell的indexValue并从yourArray中获取标签值
-(void)myAction:(UIButton *)sender
{
NSlog(@"%f",sender.tag);
NSlog(@"%@",[Passarray objectAtIndex:sender.tag]);
}
您可以从myAction推送查看控制器并将值传递给视图控制器