获取无限电话号码表查看Swift
有一个表视图,可以获取与ios联系人界面完全相同的用户电话号码问题是用户如何在textfield中添加无限的电话号码。现在用户只需在textfield中添加三个数字获取无限电话号码表查看Swift,swift,xcode,Swift,Xcode,有一个表视图,可以获取与ios联系人界面完全相同的用户电话号码问题是用户如何在textfield中添加无限的电话号码。现在用户只需在textfield中添加三个数字 let indexpath0 = IndexPath(row: 0, section: 1) let cell0 = tvInsert.cellForRow(at: indexpath0) as? InsertTableCell1 let indexpath1 = IndexPath(row: 1, section: 1) let
let indexpath0 = IndexPath(row: 0, section: 1)
let cell0 = tvInsert.cellForRow(at: indexpath0) as? InsertTableCell1
let indexpath1 = IndexPath(row: 1, section: 1)
let cell1 = tvInsert.cellForRow(at: indexpath1) as? InsertTableCell1
let indexpath2 = IndexPath(row: 2, section: 1)
let cell2 = tvInsert.cellForRow(at: indexpath2) as? InsertTableCell1
if contactModel.phoneNumbers.count == 0 {
cell0?.txtPhoneNumber.text = " "
mutableContact.phoneNumbers.removeAll()
} else if contactModel.phoneNumbers.count == 1{
mutableContact.phoneNumbers = [CNLabeledValue(label: CNLabelPhoneNumberMobile, value:CNPhoneNumber(stringValue: cell0?.txtPhoneNumber.text ?? ""))]
} else if contactModel.phoneNumbers.count == 2 {
mutableContact.phoneNumbers = [CNLabeledValue(label: CNLabelPhoneNumberMobile, value:CNPhoneNumber(stringValue: cell0!.txtPhoneNumber.text!)), CNLabeledValue(label: CNLabelPhoneNumberMobile, value:CNPhoneNumber(stringValue: cell1!.txtPhoneNumber.text!))]
} else if contactModel.phoneNumbers.count == 3{
mutableContact.phoneNumbers = [CNLabeledValue(label: CNLabelPhoneNumberMobile, value:CNPhoneNumber(stringValue: cell0!.txtPhoneNumber.text!)), CNLabeledValue(label: CNLabelPhoneNumberMobile, value:CNPhoneNumber(stringValue: cell1!.txtPhoneNumber.text!)), CNLabeledValue(label: CNLabelPhoneNumberMobile, value:CNPhoneNumber(stringValue: cell2!.txtPhoneNumber.text!))]
}
我不完全清楚您试图实现什么,但是如果您试图让用户添加电话号码,并且对字段的最大数量没有限制,那么您可以实现动态tableView 您将拥有一个电话号码数组,tableView将列出这些电话号码+一个可单击的行,该行将插入一个新电话号码
因此,tableView中的行数基本上等于PhoneNumberArray.length(数组中的电话号码数+可单击的新电话号码行)。每次点击新的电话号码行时,您都会在电话号码数组中添加一个条目,并在表格中插入一行(或重新加载表格)。您可以尝试以下代码:
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var table1Text: UITextField!
@IBOutlet weak var table2Text: UITextField!
@IBOutlet weak var table1: UITableView!
@IBOutlet weak var table2: UITableView!
var table1Data = ["a"]
var table2Data = ["1"]
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func addData(sender: AnyObject) {
//add your data into tables array from textField
table1Data.append(table1Text.text)
table2Data.append(table2Text.text)
dispatch_async(dispatch_get_main_queue(), { () -> Void in
//reload your tableView
self.table1.reloadData()
self.table2.reloadData()
})
table1Text.resignFirstResponder()
table2Text.resignFirstResponder()
}
//delegate methods
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == table1 {
return table1Data.count
}else if tableView == table2 {
return table2Data.count
}
return Int()
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if tableView == table1 {
let cell = table1.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
let row = indexPath.row
cell.textLabel?.text = table1Data[row]
return cell
}else if tableView == table2 {
let cell = table2.dequeueReusableCellWithIdentifier("Cell1", forIndexPath: indexPath) as! UITableViewCell
let row = indexPath.row
cell.textLabel?.text = table2Data[row]
return cell
}
return UITableViewCell()
}
}