ios - 如何在 objective-c 中的 View Controller 上获取自定义 TableViewCell 值
<p><p>我是 iOS 新手,我遇到了从自定义表格 View 单元格值获取值到 ViewController 的问题。</p>
<p>在我的自定义表格 View 单元格中,单击时有 4 个按钮我在 customtableviewcell 的 UILabel 中获取值我想将 UILabel 值传递给 ViewController 。 </p>
<p>我的代码在customtableviewcell.m中是这样的</p>
<pre><code>- (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
[ setBorderWidth:1.0f];
infobtn.layer.borderColor =[ 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 (!) {
lbl.hidden = NO;
}
}
- (void) textViewDidChange:(UITextView *)textView
{
if(!) {
lbl.hidden = NO;
}
else{
lbl.hidden = YES;
}
}
-(IBAction)passbtnClick:(id)sender
{
passbtn.backgroundColor=;
failbtn.backgroundColor=;
wipbtn.backgroundColor=;
nabtn.backgroundColor=;
actuallbl.text=passlbl.text;
ActualString=actuallbl.text;
NSUserDefaults *defaults=;
;
}
-(IBAction)failbtnClick:(id)sender
{
passbtn.backgroundColor=;
failbtn.backgroundColor=;
wipbtn.backgroundColor=;
nabtn.backgroundColor=;
actuallbl.text=faillbl.text;
ActualString=actuallbl.text;
NSUserDefaults *defaults=;
;
UIAlertView *testAlert = [ initWithTitle:@"Fail!"
message:audittitlelbl.text
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"Done", nil];
textView1 = ;
lbl = [ initWithFrame:CGRectMake(10.0, 0.0,90.0, 34.0)];
;
];
];
];
textView1.delegate = self;
;
;
;
}
-(IBAction)wipbtnClick:(id)sender
{
passbtn.backgroundColor=;
failbtn.backgroundColor=;
wipbtn.backgroundColor=;
nabtn.backgroundColor=;
actuallbl.text=warninglbl.text;
ActualString=actuallbl.text;
NSUserDefaults *defaults=;
;
UIAlertView *testAlert = [ initWithTitle:@"Warning!"
message:audittitlelbl.text
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"Done", nil];
textView1 = ;
lbl = [ initWithFrame:CGRectMake(10.0, 0.0,90.0, 34.0)];
;
];
];
];
textView1.delegate = self;
;
;
;
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if(buttonIndex==0)
{
}
}
-(IBAction)nabtnClick:(id)sender
{
passbtn.backgroundColor=;
failbtn.backgroundColor=;
wipbtn.backgroundColor=;
nabtn.backgroundColor=;
actuallbl.text=nalbl.text;
ActualString=actuallbl.text;
NSUserDefaults *defaults=;
;
}
</code></pre>
<p>在这段代码中,我正在更改按钮的颜色,并且我正在从 UILabel 中的 Web 服务中获得值(value),即实际。现在我想在 ViewController 中获取该值。如何执行任何建议</p>
<p> <a href="/image/tfSjX.png" rel="noreferrer noopener nofollow"><img src="/image/tfSjX.png" alt="enter image description here"/></a> </p>
<p>如图所示,我正在使用自定义表格 View 单元格,并且我希望在 ViewController 中使用 Passbtn 单击值。如果我点击 Passbtn 我在实际中得到值 1 但在自定义表格 View 单元格中,那么我怎样才能在 ViewController 中获得这个值。 </p>
<p>查看 Controller .m</p>
<pre><code> - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *STI=@"STI";
AuditTableViewCell *cell = (AuditTableViewCell *);
if (cell == nil)
{
NSArray *nib = [ loadNibNamed:@"AuditTableViewCell" owner:self options:nil];
cell = ;
cell.accessoryType=UITableViewCellAccessoryNone;
}
cell.audittitlelbl.text=];
cell.passlbl.text=];
cell.faillbl.text=];
cell.warninglbl.text=];
cell.nalbl.text=];
return cell;
}
</code></pre>
<p>提前致谢!</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您可以在 ViewController “cellForRowAtIndexPath”方法中以编程方式添加选择器。现在您可以调用 ViewController 方法,并且此方法将 sender 作为参数,然后您可以从 sender 的 superview 获取单元格。现在你有了点击了单元格按钮的单元格,你可以从单元格中获取 uilabel 文本的值。 </p>
<pre><code>-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"cell";
CartTableViewCell *cell = ;
CartModel *model = ;
cell.lblCartBrandName.text=model.strBrandName;
;
return cell;
}
-(void)btnQuantityPressed:(UIButton *)sender
{
CartTableViewCell *cell = sender.superview.superview;
NSLog("%@",cell.lblCartBrandName.text);
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 如何在 objective-c中的 ViewController 上获取自定义 TableViewCell 值,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/41499128/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/41499128/
</a>
</p>
页:
[1]