首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用selectedIndex的segmentedControl来获得相应的数组索引

如何使用selectedIndex的segmentedControl来获得相应的数组索引
EN

Stack Overflow用户
提问于 2016-04-22 03:35:07
回答 1查看 67关注 0票数 0

我的代码:

代码语言:javascript
复制
class HomeViewController: UITableViewController {
    var segmentedControl: HMSegmentedControl!
    var text = [
        // 名词
        ["音读名词","训读名词","音读与训读的分辨方法"],
        // 形容词
        ["如何分辨形容词", "常用形容词", "形容词的变形规则"]
    ]

    override func viewDidLoad() {
        super.viewDidLoad()
        configForSegmentedControl()
    }

    func configForSegmentedControl() {
        segmentedControl = HMSegmentedControl(sectionTitles: ["名词","形容词","形容动词","片假名单词","动词","复合动词"])
        segmentedControl.frame = CGRectMake(0, 0, self.view.bounds.width, 44)
        segmentedControl.segmentEdgeInset = UIEdgeInsetsMake(0, 10, 0, 10)
        segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleFullWidthStripe
        segmentedControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown
        segmentedControl.addTarget(self, action: #selector(HomeViewController.segmentedControlChangedValue(_:)), forControlEvents: UIControlEvents.ValueChanged)
        if let font = UIFont(name: "BigYoungMediumGB2.0", size: 15) {
            segmentedControl.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.blackColor(), NSFontAttributeName: font]
        }
        segmentedControl.backgroundColor = UIColor.whiteColor()
        segmentedControl.selectionIndicatorHeight = 3
        segmentedControl.selectionIndicatorColor = UIColor.blackColor()

        tableView.tableHeaderView = segmentedControl
    }

    func segmentedControlChangedValue(segmentedControl: HMSegmentedControl) {
        print("Selected index \(segmentedControl.selectedSegmentIndex) (via UIControlEventValueChanged)")
    }
}

extension HomeViewController {
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return text[segmentedControl.selectedSegmentIndex].count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        print("cellForRowAtIndexPath:\(segmentedControl.selectedSegmentIndex)")
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
        cell.textLabel?.text = text[segmentedControl.selectedSegmentIndex][indexPath.row]
        return cell
    }
}

下面是控制台输出:

cellForRowAtIndexPath:0 cellForRowAtIndexPath:0 cellForRowAtIndexPath:0 选定索引1(通过UIControlEventValueChanged) 选定索引0(通过UIControlEventValueChanged)

cellForRowAtIndexPathsegmentedControlChangedValue之前被调用,这就是为什么我不能获得正确的索引来获取文本数组的元素。那我该怎么办?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-22 04:44:15

备注:如果您想要cellForRow中的值,那么您应该重新加载'segmentedControlChangedValue‘中的表视图

通常情况下,要根据选择/操作设置单元格,应根据需要重新加载/beginUpdates表视图。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36784357

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档