gpt4 book ai didi

ios - 在 TableView 外传递和访问对象数据

转载 作者:行者123 更新时间:2023-11-28 20:58:42 26 4
gpt4 key购买 nike

我想在我的按钮操作事件中访问我在 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;
}

我的按钮Action方法-

- (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 处显示错误。

最佳答案

cellForRowAtIndexPath 方法中添加 Tag 并创建 Button 操作。

- (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];
}

关于ios - 在 TableView 外传递和访问对象数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51020659/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com