Ios 快速访问UITableView中动态单元格中的UILabel
我创建了一个原型单元,并将其用作dynamic UITableView的模板:Ios 快速访问UITableView中动态单元格中的UILabel,ios,xcode,uitableview,dynamic,Ios,Xcode,Uitableview,Dynamic,我创建了一个原型单元,并将其用作dynamic UITableView的模板: 如何访问单元格中的UIButtons和UILabels以设置每个单元格的内容和自定义操作?在您的单元格中,使用标记获取按钮和标签(可在情节提要中给出) 在您的按钮操作中根据标记值执行操作 func expandButtonClicked(sender: UIButton) { var btnTag = sender.tag if(btnTag == 1) { // Action 1
如何访问单元格中的UIButtons和UILabels以设置每个单元格的内容和自定义操作?在您的
单元格中,使用标记获取按钮和标签(可在情节提要中给出)
在您的按钮操作中
根据标记值执行操作
func expandButtonClicked(sender: UIButton) {
var btnTag = sender.tag
if(btnTag == 1) {
// Action 1
}
else if(btnTag == 2) {
// Action 2
}
}
首先,您需要用插座声明UITableViewCell的子类,并将它们与原型连接起来
class MyCustomCell: UITableViewCell {
@IBOutlet weak var label1: UILabel!
@IBOutlet weak var label2: UILabel!
@IBOutlet weak var label3: UILabel!
}
然后您的tableView(tableView:cellForRowAtIndexPath:)方法将如下所示:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: UITableViewCell? = tableView.dequeueReusableCellWithIdentifier("cell id")
if (cell == nil) {
cell = MyCustomCell()
}
(cell as MyCustomCell).label1.text = "Some text"
(cell as MyCustomCell).label2.text = "Some text"
(cell as MyCustomCell).label3.text = "Some text"
return cell;
}
通过重写UITableViewDelegate的tableView(tableView:,DidSelectRowatineIndexPath:)方法,可以为每个单元格添加自定义操作:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
switch(indexPath.row) {
case 1:
action1()
case 2:
action2()
//and so on
}
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
switch(indexPath.row) {
case 1:
action1()
case 2:
action2()
//and so on
}
}