Swift-自定义SectionIndexTitles在Tableview中不起作用

甚至工作

我有一个UISplitViewController,我已经用UIViewController替换了UITableView。该ViewController包含一个UITableView和一个UIView。UIView包含我的自定义SectionIndexTitles UIControl,以滚动到右侧部分。

我的自定义SectionIndexTitles UIControl可以在带有UICollectionView的ViewController中完美地工作,但是在前面提到的配置中它不起作用。我该如何解决?

该应用程序会编译,不会崩溃。当我擦洗UIControl时,不会调用indexViewValueChanged()函数。我已经使用View Debugger检查了View层次结构,但是它在TableView之上。我还尝试添加一个确实有效的手势识别器。最后,我尝试设置zPosition,但这没有任何效果。

Github示例

ViewController

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var indexTitlesContainer: UIView!

    /* type to represent table items
    `section` stores a `UITableView` section */
    class User: NSObject {
        let name: String
        var section: Int?

        init(name: String) {
            self.name = name
        }
    }

    // custom type to represent table sections
    class Section {
        var users: [User] = []

        func addUser(user: User) {
            self.users.append(user)
        }
    }

    // raw user data
    let names = [
        "Clementine",
        "Tim",
        "Bessie",
        "Yolande",
        "Tynisha",
        "Ellyn",
        "Trudy",
        "Fredrick",
        "Letisha",
        "Ariel",
        "Bong",
        "Jacinto",
        "Dorinda",
        "Aiko",
        "Loma",
        "Augustina",
        "Margarita",
        "Jesenia",
        "Kellee",
        "Annis",
        "Charlena"
    ]

    // `UIKit` convenience class for sectioning a table
    let collation = UILocalizedIndexedCollation.currentCollation()


    // table sections
    var sections: [Section] {
        // return if already initialized
        if self._sections != nil {
            return self._sections!
        }

        // create users from the name list
        let users: [User] = names.map { name in
            let user = User(name: name)
            user.section = self.collation.sectionForObject(user, collationStringSelector: "name")
            return user
        }

        // create empty sections
        var sections = [Section]()
        for i in 0..<self.collation.sectionIndexTitles.count {
            sections.append(Section())
        }

        // put each user in a section
        for user in users {
            sections[user.section!].addUser(user)
        }

        // sort each section
        for section in sections {
            section.users = self.collation.sortedArrayFromArray(section.users, collationStringSelector: "name") as! [User]
        }

        self._sections = sections

        return self._sections!

    }
    var _sections: [Section]?

    var indexTitles: IndexTitles!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        indexTitles = IndexTitles(frame: CGRectZero, indexTitles: collation.sectionIndexTitles)

        indexTitles.addTarget(self, action: "indexViewValueChanged:", forControlEvents: .ValueChanged)
        indexTitlesContainer.addSubview(indexTitles)

    }

    func indexViewValueChanged(sender: IndexTitles) {
        print("perform function")
        var index = sender.index

        if self.sections[index].users.count == 0 {

            var shouldPerform = true

            for var i = index as Int; i < self.sections[index].users.count; i++ {
                if shouldPerform == true {
                    if self.sections[index].users.count != 0 {
                        shouldPerform = false
                        index = i
                    }
                }
            }

            for var i = index as Int; i > 0; i-- {
                if shouldPerform == true {
                    if self.sections[index].users.count != 0 {
                        shouldPerform = false
                        index = i
                    }
                }
            }
        }

        let path = NSIndexPath(forRow: 0, inSection: index)
        tableView.scrollToRowAtIndexPath(path, atScrollPosition: .Top, animated: false)

        let scrollPosition = tableView.contentOffset.y
        let collectionViewHeight = (tableView.contentSize.height - tableView.bounds.size.height) + 54

        if scrollPosition != collectionViewHeight {
            tableView.contentOffset = CGPoint(x: tableView.contentOffset.x, y: tableView.contentOffset.y - 22.0)
        }

    }

    override func viewDidLayoutSubviews() {
        let height = CGFloat(collation.sectionIndexTitles.count * 18)
        let y = (indexTitlesContainer.bounds.height - height) / 3
        indexTitles.frame = CGRectMake(0, y, indexTitlesContainer.bounds.width, height)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

extension ViewController:UITableViewDataSource {
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return self.sections.count
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.sections[section].users.count
    }

    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return collation.sectionIndexTitles[section]
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let user = self.sections[indexPath.section].users[indexPath.row]

        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
        cell.textLabel!.text = user.name
        cell.backgroundColor = UIColor.clearColor()
        return cell
    }
}

自定义UIControl

import UIKit

class IndexTitles: UIControl,UIGestureRecognizerDelegate {

    var indexTitles:NSArray!
    var visualEffect: UIVisualEffectView!
    var index:Int!

    func translate(touch:CGFloat) {
        let selectedIndex = Int(touch / 18)
        let indexCount = indexTitles.count - 1

        if selectedIndex < 0 {
            index = 0
        }
        else if selectedIndex > indexCount {
            index = indexCount
        }
        else {
            index = selectedIndex
        }

        sendActionsForControlEvents(.ValueChanged)
    }

    //Sets the right index when the label is selected.
    override func beginTrackingWithTouch(touch: UITouch, withEvent event: UIEvent?) -> Bool {
        translate(touch.locationInView(self).y)
        return true
    }

    override func continueTrackingWithTouch(touch: UITouch, withEvent event: UIEvent?) -> Bool {
        translate(touch.locationInView(self).y)
        return true
    }

    //Setup for when the view is being initialized.
    init(frame: CGRect, indexTitles:NSArray) {
        super.init(frame: frame)
        self.indexTitles = indexTitles

        let multiplier = 18

        visualEffect = UIVisualEffectView(frame: CGRectMake(0, 0, 18, CGFloat(multiplier * indexTitles.count)))
        visualEffect.effect = UIVibrancyEffect(forBlurEffect: UIBlurEffect(style: .ExtraLight))
        self.addSubview(visualEffect)

        for var i = 0; i < indexTitles.count; i++ {
            let label = UILabel(frame: CGRectMake(0, CGFloat(multiplier * i) , 20 , 18))
            label.font = UIFont.systemFontOfSize(12, weight: UIFontWeightSemibold)
            label.numberOfLines = 0
            label.textAlignment = .Center
            label.text = indexTitles[i] as? String
            visualEffect.contentView.addSubview(label)
        }

    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }


}

具有TableView和自定义UIControl的SplitViewController

甚至工作

在UIControl中,子视图UserInteractionEnabled必须设置为false

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

自定义字体在Swift / Xcode中不起作用

来自分类Dev

自定义标签导航项不起作用-Swift

来自分类Dev

自定义Segue中的NavigationController.pushViewController不起作用-Swift 2.0-iOS 9.1

来自分类Dev

自定义Segue中的NavigationController.pushViewController不起作用-Swift 2.0-iOS 9.1

来自分类Dev

自定义 UITableViewCell heightForRowAt 在 Swift 4 中不起作用

来自分类Dev

Swift的Intercom.io自定义属性不起作用

来自分类Dev

UITableviewCell 只切换一个自定义复选标记状态不起作用 - Swift 3

来自分类Dev

TableView单元格中的Swift按钮回调不起作用

来自分类Dev

scheduleLocalNotification在Swift 2.0中不起作用

来自分类Dev

Swift中的可选分配不起作用?

来自分类Dev

UITextField inputView在Swift中不起作用

来自分类Dev

功能在Swift中不起作用

来自分类Dev

UIAlertView在Swift中不起作用

来自分类Dev

在Swift中创建块不起作用

来自分类Dev

perfrmSegueWithIdentifier()在Swift iOS中不起作用

来自分类Dev

presentViewController在Swift中不起作用

来自分类Dev

UIDevice在Swift 2.0中不起作用

来自分类Dev

CoreAudio AudioObjectRemovePropertyListener在Swift中不起作用

来自分类Dev

AVSpeechSynthesizer isSpeaking在Swift中不起作用

来自分类Dev

功能在Swift中不起作用

来自分类Dev

UIAlertView在Swift中不起作用

来自分类Dev

SKTransition在Swift中不起作用

来自分类Dev

perfrmSegueWithIdentifier()在Swift iOS中不起作用

来自分类Dev

.sort在Swift 2.0中不起作用

来自分类Dev

Swift 中的延迟/睡眠不起作用

来自分类Dev

“hasPrefix”在 Swift 中不起作用

来自分类Dev

pushViewController 在 swift 3 中不起作用

来自分类Dev

Swift - UIAlert 在 UICollectionViewCell 中不起作用

来自分类Dev

Swift:在tableView单元格中设置标签和自定义参数

Related 相关文章

  1. 1

    自定义字体在Swift / Xcode中不起作用

  2. 2

    自定义标签导航项不起作用-Swift

  3. 3

    自定义Segue中的NavigationController.pushViewController不起作用-Swift 2.0-iOS 9.1

  4. 4

    自定义Segue中的NavigationController.pushViewController不起作用-Swift 2.0-iOS 9.1

  5. 5

    自定义 UITableViewCell heightForRowAt 在 Swift 4 中不起作用

  6. 6

    Swift的Intercom.io自定义属性不起作用

  7. 7

    UITableviewCell 只切换一个自定义复选标记状态不起作用 - Swift 3

  8. 8

    TableView单元格中的Swift按钮回调不起作用

  9. 9

    scheduleLocalNotification在Swift 2.0中不起作用

  10. 10

    Swift中的可选分配不起作用?

  11. 11

    UITextField inputView在Swift中不起作用

  12. 12

    功能在Swift中不起作用

  13. 13

    UIAlertView在Swift中不起作用

  14. 14

    在Swift中创建块不起作用

  15. 15

    perfrmSegueWithIdentifier()在Swift iOS中不起作用

  16. 16

    presentViewController在Swift中不起作用

  17. 17

    UIDevice在Swift 2.0中不起作用

  18. 18

    CoreAudio AudioObjectRemovePropertyListener在Swift中不起作用

  19. 19

    AVSpeechSynthesizer isSpeaking在Swift中不起作用

  20. 20

    功能在Swift中不起作用

  21. 21

    UIAlertView在Swift中不起作用

  22. 22

    SKTransition在Swift中不起作用

  23. 23

    perfrmSegueWithIdentifier()在Swift iOS中不起作用

  24. 24

    .sort在Swift 2.0中不起作用

  25. 25

    Swift 中的延迟/睡眠不起作用

  26. 26

    “hasPrefix”在 Swift 中不起作用

  27. 27

    pushViewController 在 swift 3 中不起作用

  28. 28

    Swift - UIAlert 在 UICollectionViewCell 中不起作用

  29. 29

    Swift:在tableView单元格中设置标签和自定义参数

热门标签

归档