Ios indexPathForSelectedRow始终为选定的节和行返回0
我正在使用Ios indexPathForSelectedRow始终为选定的节和行返回0,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我正在使用UITableViewCell开发iOS应用程序。 当点击UITableViewCell并转到另一个带有segue的屏幕时,indexPathForSelectedRow始终为所选节和行返回0 我的代码如下 你能告诉我怎么解决这个问题吗 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 2; } - (NSInteger)tableView:(UITableView *)t
UITableViewCell
开发iOS应用程序。
当点击UITableViewCell
并转到另一个带有segue
的屏幕时,indexPathForSelectedRow
始终为所选节和行返回0
我的代码如下
你能告诉我怎么解决这个问题吗
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(section == 0) {
return 3;
}
else if(section == 1) {
return 2;
}
return 0;
}
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 50.0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellIdentifer = @"editCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer forIndexPath:indexPath];
if(cell==nil){
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer];
}
UITextView *editText = (UITextView *)[cell viewWithTag:1];
if(indexPath.section == 0) {
if(indexPath.row == 0) {
editText.text = @"aaa";
} else if(indexPath.row == 1){
editText.text = @"bbb";
} else {
editText.text = @"ccc";
}
}
else if(indexPath.section == 1) {
if(indexPath.row == 0) {
editText.text = @"ddd";
} else if(indexPath.row == 1){
editText.text = @"eee";
}
}
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"editDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSLog(@"a%d",indexPath.section);
NSLog(@"b%d",indexPath.row);
}
看看你的代码,你不见了
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
从该方法执行转换,您将可以直接访问indexPath您可能会在代码中的某个位置取消选择行。最有可能在
didSelectRowAtIndexPath
方法中。通常的做法是取消选择刚刚选定的行。如果取消选择该行,则在尝试获取indexPath.row.时,这将导致在prepareForSegue
方法中获得0。作为旁白,一段时间以来,不再需要在cellforrowatinexpath
方法中检查If(cell==nil)
。请查看此链接,谢谢您的回复。我在你的链接中尝试了这种方法。但是,我的问题没有解决。单元格显示正确。您的代码看起来正常,所以我有几个问题。。。1.NSLog
呼叫是否返回终端中的任何线路?;2.您正在使用故事板,因此您是否检查了segue标识符值是否确实输入为“editDetail”?感谢您的回复。通过您的方法,我可以在[-(void)tableView:(UITableView*)tableView didselectrowatinexpath:(nsindepath*)indepath]中获得正确的节值和行值。但是,我无法在[-(void)prepareforsgue:(UIStoryboardSegue*)segue sender:(id)sender]中获取collect值。您能告诉我如何将正确的值转换到下一个屏幕吗?您可以将标识符添加到您的segue,然后调用[self-PerformsgueWithIdentifier:@“mySegue”发送者:self];从didSelectRowAtIndexPath方法