我想访问在按钮动作事件中在tableview方法中使用的对象数据,它显示错误-“使用未声明的标识符”, 这是我的代码段-
TableView方法-
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"BlogTableViewCell";
BlogTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
long index1 = 2 * (indexPath.row);
long index2 = index1 + 1;
if (index1 < [self.BlogsArray count]) {
cell.vw_blog1.tag = index1;
BlogModel *blog = [self.BlogsArray objectAtIndex:index1];
cell.lbl_blogTitle1.text = blog.blogTitle;
cell.lbl_blogDescripton1.text = blog.blogDesc;
}
我的按钮动作方法-
- (IBAction)btnAction_AuthorName1:(id)sender {
NSLog(@"Author1 of blog1 is clicked");
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Consult" bundle:nil];
DRDetailsViewController *DRDetailVC = (DRDetailsViewController *)[storyboard instantiateViewControllerWithIdentifier:@"DRDetailsViewController"];
DRDetailVC.DoctorID = [NSString stringWithFormat:@"%@",blog.userId];
[APP_DELEGATE.navigationController pushViewController:DRDetailVC animated:YES];
}
在blog.UserId处显示错误。
答案 0 :(得分:1)
添加Tag
并以Button
方法创建cellForRowAtIndexPath
操作。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"BlogTableViewCell";
BlogTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.btn.tag = indexPath.row;
[cell.btn addTarget:self action:@selector(btnAction_AuthorName1:) forControlEvents:UIControlEventTouchUpInside];
}
按钮操作代码。
- (IBAction)btnAction_AuthorName1:(UIButton *)sender {
BlogModel *blog = [self.BlogsArray objectAtIndex:sender.tag];
NSLog(@"Author1 of blog1 is clicked");
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Consult" bundle:nil];
DRDetailsViewController *DRDetailVC = (DRDetailsViewController *)[storyboard instantiateViewControllerWithIdentifier:@"DRDetailsViewController"];
DRDetailVC.DoctorID = [NSString stringWithFormat:@"%@",blog.userId];
[APP_DELEGATE.navigationController pushViewController:DRDetailVC animated:YES];
}
答案 1 :(得分:1)
在cellForRowAtIndexPath方法中为按钮添加标签。
cell.btn.tag = indexPath.row;
,然后获取所选索引的标签值
- (IBAction)btnAction_AuthorName1:(UIButton *)sender {
BlogModel *blog = [self.BlogsArray objectAtIndex:sender.tag];
NSLog(@"Author1 of blog1 is clicked");
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Consult" bundle:nil];
DRDetailsViewController *DRDetailVC = (DRDetailsViewController *)[storyboard instantiateViewControllerWithIdentifier:@"DRDetailsViewController"];
DRDetailVC.DoctorID = [NSString stringWithFormat:@"%@",blog.userId];
[APP_DELEGATE.navigationController pushViewController:DRDetailVC animated:YES];
}