简体   繁体   English

选择了UItableview部分-迅速

[英]UItableview section selected - swift

''Hello World'' :) I have a UItableview with 4 different sections. ” Hello World” :)我有一个包含4个不同部分的UItableview。 I'm trying to fill each sections with some rows depending on the section selected. 我试图根据选定的部分用一些行填充每个部分。 I am struggling -__- to detect which section is selected in order to fill them accordingly. 正在 -__-努力检测选择了哪个部分以便相应地填充它们。 In my program I have created 4 uibuttons (outside of the tableview and they represents each user; when a button is selected I use them to name the section header). 在我的程序中,我创建了4个uibutton(在表视图之外,它们代表每个用户;选择按钮时,我使用它们来命名节标题)。 Now I would like to link the button selected with the section. 现在,我想将所选按钮链接到该部分。 How one should proceed to know which section is selected. 应该如何知道所选的部分。 If button A is selected it would mean that section 0 is selected then do operation x in section 0 (like inserting a new row for example); 如果选择了按钮A,则意味着选择了第0部分,然后在第0部分中进行了x操作(例如,插入新行); if button B is selected (ie section 1 is selected) then Do operation y and so on... 如果选择了按钮B(即选择了第1部分),则执行操作y,依此类推...

Thanks in advance for helping a desperate soul 预先感谢您帮助绝望的灵魂

Set tags for buttons. 设置按钮标签。

button1.tag = 0
button2.tag = 1
button3.tag = 2
button4.tag = 3

then add targets 然后添加目标

button1.addTarget(self, action: #selector(didTouchButton(_:)), for: .touchUpInside)
button2.addTarget(self, action: #selector(didTouchButton(_:)), for: .touchUpInside)
button3.addTarget(self, action: #selector(didTouchButton(_:)), for: .touchUpInside)
button4.addTarget(self, action: #selector(didTouchButton(_:)), for: .touchUpInside)

then when button will select check sender tag and do operation on section 然后当按钮将选择检查发件人标签并在该部分进行操作时

func didTouchButton(_ sender: UIButton) {
    let section = sender.tag

    //do operations in section
}

Here are examples of inserting and deleting rows in section based on section index. 是基于节索引在节中插入和删除行的示例。 In Swift3 you should base on this methods: 在Swift3中,您应该基于以下方法:

func insertRows(at: [IndexPath], with: UITableViewRowAnimation)
func deleteRows(at: [IndexPath], with: UITableViewRowAnimation)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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