【问题标题】:PrepareForSegue for dynamic Array in UITableViewUITableView 中动态数组的 PrepareForSegue
【发布时间】:2016-07-10 07:08:25
【问题描述】:

我想以编程方式从TableView 开始。单元格的内容是动态的 - 正在发生变化,所以我无法将 segue 包装到行数(例如indexPath.row = 1)。我的数组是这样的:

myArray = [value1, value2, value3]

但是 value1 可以是今天的“A”,但明天将是“B”。所以今天 value1 应该重定向到 AController,但明天 - 到 BController。值的名称当然显示在TableView中。

我想prepareForSegue 应该基于值的名称(例如,如果值的名称是“A”,那么...)。但我不知道方法。

任何帮助将不胜感激:)

更清楚一点 - 我的数组是如何生成的:

let cal = NSCalendar.currentCalendar()
let fmt = NSDateFormatter()
var countDays = [String]()

override func viewDidLoad() {
    super.viewDidLoad()

    fmt.dateFormat = "EEEE"
    fmt.locale = NSLocale(localeIdentifier: "pl_PL")

    var date = cal.startOfDayForDate(NSDate())

    while countDays.count < 7 {
       let weekDay = cal.component(.Weekday, fromDate: date)
       if weekDay != 0 {
            countDays.append(fmt.stringFromDate(date))
       }
       date = cal.dateByAddingUnit(.Day, value: 1, toDate: date, options: NSCalendarOptions(rawValue: 0))!
    }
    print(countDays)

【问题讨论】:

  • 工作日比较完全没用,因为工作日索引永远不会为0。
  • 为什么没用?它对我有用。
  • 根据定义weekDay永远不会为零,所以检查总是成功的。

标签: ios iphone arrays swift uitableview


【解决方案1】:

您可以使用 tableview 的“didselectRowAtIndex”委托方法。您将获得用户选择/单击的单元格的当前索引路径。使用此索引路径检索数组中的相应对象。接下来使用 if/else 检查检索到的对象“A”或“B”中的值,具体取决于此您可以启动您的“AController”或“Controller”。使用 prepare for segue 启动您的特定控制器。

注意:所有这些逻辑都应该在你的“didselectRowAtIndex”方法中完成。

希望这会有所帮助。

【讨论】:

  • 是的,我知道我应该使用 didselectRowAtIndex,但我的问题是,如何在行中请求特定的字符串。应该是这样的:“if cell.textLabel!.text == "ABC" { performSegueWithIdentifier("segueABC", sender:self) ??
  • 正如我在回答中提到的,您应该使用“index path.row”从表视图数组中检索对象。此对象与您用于填充单击/选定单元格的对象相同。现在这个对象应该有你正在谈论的“字符串”。 if(. == "A"){performSegue("SegueA")}
  • 你能告诉我你会使用的方法吗?
  • 数组 *tableViewArray = @{A,B,C} ; selectedObject = tableViewArray.objectAtIndex[indexpath.row];这就是您检索所选对象的方式
【解决方案2】:

好的,我用代码解决了这个问题:

让单元格:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)! 让 str: String = cell.textLabel!.text!

    if str.containsString("A") {
        performSegueWithIdentifier("AMonday", sender:self)
    }

很简单,但是发现这很痛苦;)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-29
    • 1970-01-01
    • 2012-04-24
    • 1970-01-01
    • 2019-02-06
    相关资源
    最近更新 更多