Как я могу использовать watchconnectivity для передачи данных таблицы из IOS в watchOS4?
Я очень новичок в использовании Swift и полностью запутался в WatchConnectivity. iOS ViewController:
import UIKit
импорт WatchConnectivity
Класс 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)
}
}
расширение ViewController: UITableViewDataSource { func numberOfSections(в 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 Импорт 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()
}
}
Может кто-нибудь помочь мне, что мне сделать, чтобы соединить две таблицы?
1 ответ
Я работал над WatchConnectivity. Сначала я тоже заблудился, пока не нашел интересную статью о WatchConnectivity Ральфа Эберта.
Я надеюсь, что это поможет вам. Вот ссылка на его блог.
http://www.ralfebert.de/tutorials/watchos-watchkit-connectivity/
Любые проблемы, пожалуйста, дайте мне знать.
Kimchheang
Пномпень, Камбоджа