如何在选择 UITableViewCell 行中传递值
Posted
技术标签:
【中文标题】如何在选择 UITableViewCell 行中传递值【英文标题】:How to pass value in select UITableViewCell Row 【发布时间】:2016-07-08 05:37:46 【问题描述】:我有UITableViewCell
,单元格索引类别明智并展开单元格单击标题,并通过 JSON 加载数据。我想选择单元格并将数据传递给另一个视图控制器。我尝试了很多次,但只传递数据第一个单元格。我单击了另一个单元格,因此此条件将数据传递给第一个单元格。无法发送特定的单元格数据。怎么可能。请帮忙。
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
NSError *error;
NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
NSArray *statuses = [json objectForKey:@"status"];
names=[[NSMutableArray alloc]initWithArray:[statuses valueForKey:@"business_category_name"]];
business_details_array=[[NSMutableArray alloc]initWithArray:[statuses valueForKey:@"business_details"]];
for (int i=0; i<[names count]; i++)
[arrayForBool addObject:[NSNumber numberWithBool:NO]];
[self.tabel_view reloadData];
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
return names.count;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
if ([[arrayForBool objectAtIndex:section] boolValue])
return [[business_details_array objectAtIndex:section] count];
else
return 0;
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
return 70;
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
UIView *sectionHeaderView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width,70)];
sectionHeaderView.backgroundColor=[UIColor grayColor];
sectionHeaderView.tag=section;
UIView *sectionsubHeaderView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width,60)];
sectionsubHeaderView.backgroundColor=[UIColor blueColor];
UIImageView *arrow=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0,60, 60)];
[arrow setImage:[UIImage imageNamed:@"arrow.png"]];
UILabel *Lbl=[[UILabel alloc]initWithFrame:CGRectMake(60, 0,tableView.frame.size.width-60, 60)];
Lbl.text=[names objectAtIndex:section];
Lbl.textColor=[UIColor whiteColor];
[sectionsubHeaderView addSubview:arrow];
[sectionsubHeaderView addSubview:Lbl];
[sectionHeaderView addSubview:sectionsubHeaderView];
UITapGestureRecognizer *headerTapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionHeaderTapped:)];
[sectionHeaderView addGestureRecognizer:headerTapped];
return sectionHeaderView;
- (void)sectionHeaderTapped:(UITapGestureRecognizer *)gestureRecognizer
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:gestureRecognizer.view.tag];
if (indexPath.row == 0)
BOOL collapsed = [[arrayForBool objectAtIndex:indexPath.section] boolValue];
for (int i=0; i<[names count]; i++)
if (indexPath.section==i)
[arrayForBool replaceObjectAtIndex:i withObject:[NSNumber numberWithBool:!collapsed]];
[self.tabel_view reloadSections:[NSIndexSet indexSetWithIndex:gestureRecognizer.view.tag] withRowAnimation:UITableViewRowAnimationAutomatic];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
MemberTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ht"];
if (cell==nil)
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Cell" owner:self options:nil];
cell = [nib objectAtIndex:0];
NSString*title_str=[NSString stringWithFormat:@"%@",[[[business_details_array objectAtIndex:indexPath.section] valueForKey:@"name"] objectAtIndex:indexPath.row]];
cell.title.text= title_str;
[titles addObject:title_str];
NSLog(@"get %@",titles);
cell.email.text=[NSString stringWithFormat:@"%@",[[[business_details_array objectAtIndex:indexPath.section] valueForKey:@"email"] objectAtIndex:indexPath.row]];
//[emailary addObject:cell.email.text];
cell.address_lbl.text=[NSString stringWithFormat:@"%@",[[[business_details_array objectAtIndex:indexPath.section] valueForKey:@"address"] objectAtIndex:indexPath.row]];
//[adrsary addObject:cell.address_lbl.text];
cell.phone_lbl.text=[NSString stringWithFormat:@"%@",[[[business_details_array objectAtIndex:indexPath.section] valueForKey:@"phone"] objectAtIndex:indexPath.row]];
// [phoneary addObject:cell.phone_lbl.text];
cell.web_lbl.text=[NSString stringWithFormat:@"%@",[[[business_details_array objectAtIndex:indexPath.section] valueForKey:@"website"] objectAtIndex:indexPath.row]];
//[websiteary addObject:cell.web_lbl.text];
cell.sens_lbl.text=[NSString stringWithFormat:@"%@",[[[business_details_array objectAtIndex:indexPath.section] valueForKey:@"member_since"] objectAtIndex:indexPath.row]];
//[sensary addObject:cell.sens_lbl.text];
cell.des_lbl.text=[NSString stringWithFormat:@"%@",[[[business_details_array objectAtIndex:indexPath.section] valueForKey:@"des"] objectAtIndex:indexPath.row]];
//[desary addObject:cell.des_lbl.text];
NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: [NSString stringWithFormat:@"%@",[[[business_details_array objectAtIndex:indexPath.section] valueForKey:@"img_url"] objectAtIndex:indexPath.row]]]];
UIImage* image = [[UIImage alloc] initWithData:imageData];
cell.image_view.image =image;
//[images addObject:cell.image_view.image];
return cell;
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
[arrayForBool replaceObjectAtIndex:indexPath.section withObject:[NSNumber numberWithBool:NO]];
[self.tabel_view reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
if ([segue.identifier isEqualToString:@"showRecipeDetail"])
NSIndexPath *indexPath = [self.tabel_view indexPathForSelectedRow];
member_details *destViewController = segue.destinationViewController;
destViewController.hello = [titles objectAtIndex:indexPath.row];
【问题讨论】:
嗨,Kumawat 正在使用导航控制器来推送下一个视图吗? @KoushikGounder 是的,正确唱下一个视图控制器 所以您需要单击一个单元格,然后会出现一个更大的视图,其中包含该特定单元格的详细信息,我是否正确?如果我错了请纠正我以便我可以提供完美的解决方案 是的,你是对的 如果您在这里发现任何困难评论,请尝试我的答案 【参考方案1】:由于您已直接从 tableview 加入 segue,因此这样做不是正确的方法。而不是你必须通过控制器加入到控制器中。因此,在获得正确选择的行索引之前,您的视图控制器会快速推送,并且您总是获得 0 索引。
您可以在UITablView
的didSelect
方法中执行相同的操作,而不是在prepareForSegue
中编写逻辑。
为此,您必须从情节提要中找到目标视图控制器。并将数据设置为所需的属性。
编辑
删除您的准备安全逻辑并在 didSelect 中执行此操作
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
[arrayForBool replaceObjectAtIndex:indexPath.section withObject:[NSNumber numberWithBool:NO]];
[self.tabel_view reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];
UIStoryboard *storyboard =
[UIStoryboard storyboardWithName:@"MainStoryboard"
bundle:[NSBundle mainBundle]];
YourDestinationVC *vc =[[storyboard instantiateViewControllerWithIdentifier:@"YourDestinationVC"]; //@"YourDestinationVC" is storyboardID for your destination view controller
vc.hello = [titles objectAtIndex:indexPath.row];
[[self navigationController] pushViewController:vc animated:YES];
这是截图:
【讨论】:
@A.Kumawat 也从情节提要中删除 segue。 Instedof 将 stroyboardID 提供给 viewcontroller。并在代码中使用这个storyboardID。 @A.Kumawat 如果您仍然遇到问题,请告诉我。 错误显示如下:- 不包含标识符为“YourDestinationVC”的视图控制器 @"YourDestinationVC" 是你的目标视图控制器的storyboardID 你必须用你的视图控制器替换它【参考方案2】:您应该在视图控制器之间创建转场。
不要创建从单元到视图控制器的 segue。
那么你应该调用
[self performSegueWithIdentifier:@"showRecipeDetail" sender:self];
更新didSelectRowAtIndexPath
中的表后
【讨论】:
我添加了这一行,但无法通过。怎么可能【参考方案3】:试试这个删除准备prepareForSegue
和didselectrowatindexpath
添加。
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
[arrayForBool replaceObjectAtIndex:indexPath.section withObject:[NSNumber numberWithBool:NO]];
[self.tabel_view reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];
member_details *destViewController = [[member_details alloc]init];
destViewController.hello = [titles objectAtIndex:indexPath.row];
[[self navigationController] pushViewController:destViewController animated:YES];
【讨论】:
在这个无标识符集中? 这是通过编程不使用故事板来实现的 照我说的在 tempVC.m 中创建一个视图控制器 (tempVC) ` -(void)viewDidLoad [super viewDidLoad]; [self.view setBackgroundColor:[UIColor greenColor]]; ` 在你的 tableviewcontrollers .h 文件中导入 tempVC 在你的 didselectrowatindex 路径中创建一个对象 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath tempVC *destViewController = [[tempVC alloc]init]; [[self navigationController] pushViewController:destViewController 动画:YES]; 如果您在单击单元格后看到 greenColor,请发表评论 与您的代码相同,但无法传递数据。怎么可能请帮忙 你得到绿色视图了吗?【参考方案4】:试试这个
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
[self performSegueWithIdentifier:@"showRecipeDetail" sender:[YOUR_ARRAY objectAtIndex:indexPath.row]];
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
if ([segue.identifier isEqualToString:@"showRecipeDetail"])
member_details *destViewController = segue.destinationViewController;
destViewController.hello = sender;
【讨论】:
以上是关于如何在选择 UITableViewCell 行中传递值的主要内容,如果未能解决你的问题,请参考以下文章
在 UITableViewCell 中编辑 TextLabel [重复]
如何在单元格选择/取消选择时正确切换 UITableViewCell 的 accesoryType?