简体   繁体   中英

How can I use watchconnectivity to transfer table data from IOS to watchOS4?

I'm very new to using Swift and completely confused with WatchConnectivity. iOS ViewController:

import UIKit

import WatchConnectivity

class ViewController: UIViewController {

@IBOutlet weak var tableView: UITableView!

var courses = [String]()

override func viewDidLoad() {
    super.viewDidLoad()

    }

@IBAction func onAddTapped() {

    let alert = UIAlertController(title: "Add Course", message: nil, preferredStyle: .alert)
    alert.addTextField { (courseTF) in
       courseTF.placeholder = "Enter Course"
    }
    let action = UIAlertAction(title: "Add", style: .default) { (_) in
        guard let course = alert.textFields?.first?.text else { return }
        print(course)
        self.add(course)
    }
    alert.addAction(action)
    present(alert, animated: true)
}

func add(_ course: String) {
    let index = 0
    courses.insert(course, at: index)

    let indexPath = IndexPath(row: index, section: 0)
    tableView.insertRows(at: [indexPath], with: .left)

    }
}

extension ViewController: UITableViewDataSource { func numberOfSections(in tableView: UITableView) -> Int { return 1 }

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return courses.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell()
    let course = courses[indexPath.row]
    cell.textLabel?.text = course
    return cell
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    guard editingStyle == .delete else { return }
    courses.remove(at: indexPath.row)

    tableView.deleteRows(at: [indexPath], with: .automatic)
}

}

WatchOs InterfaceController:

import WatchKit import Foundation import WatchConnectivity

class InterfaceController: WKInterfaceController {

@IBOutlet var table: WKInterfaceTable!
var courses = [String] ()

override func awake(withContext context: Any?) {
    super.awake(withContext: context)

    table.setNumberOfRows(courses.count, withRowType: "Row")

    for rowIndex in 0 ..< courses.count  {
        set(row: rowIndex, to: courses [rowIndex])

    }
}

func set(row rowIndex: Int, to text: String) {
    guard let row = table.rowController(at: rowIndex) as? courseInsertrow else { return }
    row.textLabel.setText(text)
}

override func willActivate() {
    // This method is called when watch view controller is about to be visible to user
    super.willActivate()
}

override func didDeactivate() {
    // This method is called when watch view controller is no longer visible
    super.didDeactivate()
}

}

can anyone help guide me on what to do to connect the two tables?

I've been working on WatchConnectivity. I also got lost at first until I found an interesting article about WatchConnectivity by Ralf Ebert .

I hope this will help you out. Here is the link to his blog.

http://www.ralfebert.de/tutorials/watchos-watchkit-connectivity/

Any concerns please let me know.
Kimchheang
Phnom Penh, Cambodia

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM