如何使用Swift从tableview单元中获取数据而不在标签上显示?

时间:2019-02-13 18:33:00

标签: swift tableview

我的场景中,我试图创建tableview单元格单击以获取label数据,但我还有更多values,但我不想在label中显示。现在,我该如何assign unshowed将数据cellsending,因为我是view controller值到单元格didselect之后的另一个 let alpha: [String] = ["A", "B", "C", "D", "E"] let numeric: [String] = ["1", "2", "3", "4", "5"]

例如:

我有两个数组值

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell:MyCustomCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! MyCustomCell
    cell.textlabel.text = alpha[indexPath.row]
    return cell
}

在这里,我只将alpha值播种到tableview单元格中

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

     let currentCell = tableView.cellForRow(at: indexPath)! as? MyCustomCell
     print(currentCell?.textlabel?.text ?? "unknown alpha")

     // Here I need to get numeric values also 
}

现在,我要获取tableview单元格选定的值,在这里,我可以获取cell.textlabel.text值,但我还需要获取数字值,而无需分配任何标签。

// Create 'Customers' queue
Queue2<Customer> Customers = new Queue2<Customer>();

// Initializations of customers
Customer customer1 = new Customer(912, 6, 15);

// Add customers to queue
Customers.enqueue(customer1);

// Customer at teller 'i'
Customer C1 = new Customer(908, 3, 20);

// Denotes if teller 'i' is busy with a customer OR a task
int B1 = 1;

// Create 'Tasks' stack
Stack Tasks = new Stack();

// Initializiations of tasks
Task task1 = new Task(910, 12);
Task task2 = new Task(913, 3);

// Add tasks to stack
Tasks.push(task1);
Tasks.push(task2);

// Give tellers 1 and 2 an empty task to start
Task EmptyTaskT1 = new Task(0,0);
Task EmptyTaskT2 = new Task(0,0);

// Current task being worked on by teller 3
Task TaskCurr = new Task(916, 3);

// Initializations of Tellers
Teller tell1 = new Teller(C1, EmptyTaskT1, B1);

// Print Teller info
System.out.println(tell1);

// Printing just C1
System.out.println(C1);

1 个答案:

答案 0 :(得分:-1)

通过使用以下代码,我们可以获得未显示的单元格索引数组值

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        let currentCell = tableView.cellForRow(at: indexPath)! as? MyCustomCell
        print(currentCell?.myCellLabel?.text ?? "unknown")

        let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let newViewController = storyBoard.instantiateViewController(withIdentifier: "detailview") as! DetailViewController
        newViewController.name = currentCell?.myCellLabel?.text ?? "unknown"
        newViewController.country = self.numeric[indexPath.row]
        self.present(newViewController, animated: true, completion: nil)
    }