【发布时间】:2017-11-13 15:19:43
【问题描述】:
我有一个自定义的 tableView,它有 2 个标签和一个按钮。 我想要做的是,当我按下特定单元格中的按钮时,将打印该单元格中标签中的文本。
我已经使用委托使按钮像这样工作。
**Protocol**
protocol YourCellDelegate : class {
func didPressButton(_ tag: Int)
}
**UITableViewCell**
class YourCell : UITableViewCell
{
weak var cellDelegate: YourCellDelegate?
// connect the button from your cell with this method
@IBAction func buttonPressed(_ sender: UIButton) {
cellDelegate?.didPressButton(sender.tag)
}
...
}
**cellForRowAt Function**
cell.cellDelegate = self
cell.tag = indexPath.row
**final Function**
func didPressButton(_ tag: Int) {
print("I have pressed a button")
}
现在我如何显示来自该特定单元格的数据
非常感谢您的帮助
编辑
-getting contacts from phone-
lazy var contacts: [CNContact] = {
let contactStore = CNContactStore()
let keysToFetch = [
CNContactFormatter.descriptorForRequiredKeys(for: .fullName),
CNContactEmailAddressesKey,
CNContactImageDataAvailableKey] as [Any]
// Get all the containers
var allContainers: [CNContainer] = []
do {
allContainers = try contactStore.containers(matching: nil)
} catch {
print("Error fetching containers")
}
var results: [CNContact] = []
// Iterate all containers and append their contacts to our results array
for container in allContainers {
let fetchPredicate = CNContact.predicateForContactsInContainer(withIdentifier: container.identifier)
do {
let containerResults = try contactStore.unifiedContacts(matching: fetchPredicate, keysToFetch: keysToFetch as! [CNKeyDescriptor])
results.append(contentsOf: containerResults)
} catch {
print("Error fetching results for container")
}
}
return results
}()
-cellForRowAt-
let cell = tableView.dequeueReusableCell(withIdentifier: "PersonCell", for: indexPath) as? PersonCell
let contacts = self.contacts[indexPath.row]
cell?.updateUI(contact: contacts)
cell?.cellDelegate = self as? YourCellDelegate
cell?.tag = indexPath.row
return cell!
【问题讨论】:
标签: ios uitableview swift3 uibutton