我正在使用 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
关于ios - indexPathForSelectedRow 始终为所选部分和行返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25183103/