Ios 将tableView单元格的indexPath传递给另一个控制器
我想将Ios 将tableView单元格的indexPath传递给另一个控制器,ios,uitableview,segue,Ios,Uitableview,Segue,我想将select行作为indepath传递给另一个控制器。为此,我编写了以下代码: func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UIT
select行作为indepath
传递给另一个控制器。为此,我编写了以下代码:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
let indexPath = tableView.indexPathForSelectedRow();
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var secondController: SecondViewController = segue.destinationViewController as SecondViewController
secondController.rowIndex.text = indexPath
}
但我的指数是零。为什么?在cellForRowAtIndexPath中,这是错误的:
let indexPath = tableView.indexPathForSelectedRow();
对于每一行,您将所选行保存在一个变量中,该变量将在cellForRowAtIndexPath完成后销毁,因为它不会被任何对象保留
你只需要在准备室里用这个
var secondController: SecondViewController = segue.destinationViewController as SecondViewController
if let selectedIndexPath = tableView.indexPathForSelectedRow();
secondController.rowIndex= selectedIndexPath.row
还没有测试,但我认为这会起作用。希望对您有所帮助。要满足您的要求,请执行以下操作
步骤1:声明globle变量(该范围在类中)
如var selectedIndexForRow:Int?=无
var selectedIndexForSection:Int?=无
步骤2:覆盖didSelectRowAtIndexPath
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
selectedIndexForRow = indexPath.row;
selectedIndexForSection = indexPath.section;
}
步骤3:将selectedIndexForRow
和selectedIndexForSection
变量传递给另一个控制器
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var secondController: SecondViewController = segue.destinationViewController as SecondViewController
secondController.row= selectedIndexForRow
secondController.section= selectedIndexForSection
}
prepareforsgue
中的sender
是您选择的UITableViewCell
只需将其键入到UITableViewCell
,然后在tableView
上调用indexPathForCell
。这将返回所选单元格的indepath
。不需要额外的变量,全局变量或其他变量
很抱歉,我不能发布任何Swift代码。我精通Obj-c,但我还不懂Swift
Obj-C的示例代码如下所示。我希望你自己写Swift,或者这里有人会编辑它
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
UITableViewCell* cell = (UITableViewCell*)sender;
NSIndexPath* indexPath = [self.tableView indexPathForCell:cell];
//Use indexPath as you intend to
}
didselectrowatindexpath在哪里?您不能将indexPath传递给NSSTRING。实际上,您被声明为“indexPath”局部变量,并且其作用域在其方法内。另外您在“cellFOrRowAtIndexPath”中也写过,这个方法是根据屏幕上显示的单元格数量调用次数。更好的是,您应该重写“DidSelectRowatineXpath”并将selected indexPath指定为全局变量。在Javier的建议中,您可能需要使用secondController.rowIndex.text=“/(selectedIndexPath)”强制转换为文本或将secondController中的rowIndex声明为NSIndexPath关于DidSelectRowatineXpath
的问题。我必须使用它而不是CellForRowatineXpath
或?是的,如果这不起作用,可能是因为您应该在'DidSelectRowatineXpath'中将单元格标记为选中,让currentCell=tableView。CellForRowatineXpath(indexPath)标记为UITableViewCell;currentCell.selected=true在CellForRowatineXpath
下,我使用let indexath=tableView.indexPathForSelectedRow()创建了一个funcdidselectRowatineXpath
;println(indexPath)
但它会崩溃因为indexPathForSelectedRow()是可选的,所以在访问它自己之前,你应该检查它是否包含任何值。你可以按照我上面写的方法检查它:如果让selectedIndexPath=tableView.indexPathForSelectedRow();nsindepath使用未声明类型nsindepath
@OrkhanAlizade,现在您可以用作var selectedIndex:Int?=nil
仅当您希望存储节时才存储行,然后创建另一个变量并遵循相同的过程。我必须更新我的答案。请检查。或者您需要更改nsindepath
的声明全局变量的语法格式,如var-selectedIndex:nsindepath?