加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

数组 – Swift中的NSIndexPath

发布时间:2020-12-14 05:29:10 所属栏目:百科 来源:网络整理
导读:我知道,希望类NSIndexPath处理具有数组的数组. 我有这个代码: import UIKitclass ViewController: UIViewController,UITableViewDataSource { let devCourses = [ ("iOS App Dev with Swift Essential Training","Simon Allardice"),("iOS 8 SDK New Featur
我知道,希望类NSIndexPath处理具有数组的数组.

我有这个代码:

import UIKit

class ViewController: UIViewController,UITableViewDataSource {

    let devCourses = [
        ("iOS App Dev with Swift Essential Training","Simon Allardice"),("iOS 8 SDK New Features","Lee Brimelow"),("Data Visualization with D3.js","Ray Villalobos"),("Swift Essential Training",("Up and Running with AngularJS",("MySQL Essential Training","Bill Weinman"),("Building Adaptive Android Apps with Fragments","David Gassner"),("Advanced Unity 3D Game Programming","Michael House"),("Up and Running with Ubuntu Desktop Linux","Scott Simpson"),("Up and Running with C","Dan Gookin") ]

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        return devCourses.count
    }

    func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        var cell = UITableViewCell()

        let (courseTitle,courseAuthor) = devCourses[indexPath.row]
        cell.textLabel?.text = courseTitle

        return cell
    }

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

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


}

我不明白NSIndexPath是如何工作的.我已经阅读了文档,但是在我学习这一点的过程中,我仍然难以理解.我如何知道NSIndexPath具有行的属性?罐头有人解释这一行的每一部分请:

let (courseTitle,courseAuthor) = devCourses[indexPath.row]

NSIndexPath如何知道此常量中的courseTitle引用索引号0 whitin数组索引0(“iOS App Dev with Swift Essential Training”,“Simon Allardice”)

What I don’t understand how NSIndexPath works.

对于iOS,如果您正在使用UITableViews,则可以将NSIndexPath视为包含两个Int属性部分和行的只读结构,如果您正在使用UICollectionViews,则可以将其视为section和item.

您可以使用NSIndexPath创建它们:forRow:inSection:factory方法:

let indexPath = NSIndexPath(forRow: 1,inSection: 0)

并通过访问他们的属性阅读它们:

print("row is (indexPath.row)")

How can I know NSIndexPath has property of row?

Xcode有一些很好的功能可以帮助您理解您正在查看的代码.在Xcode中,按住Option键点击上面语句行中的行,它会告诉你它是什么.在弹出窗口中,如果单击Reference旁边的NSIndexPath UIKit Additions,它将显示文档.

Can somebody explain every part of this line please:

let (courseTitle,courseAuthor) = devCourses[indexPath.row]

devCourses是一个元组数组,包含两个String类型的值.您可以通过选项单击devCourses来看到它,它显示[(String,String)].如果它是一个String数组的数组,它会说[[String]]或[Array< String>](这是两种说法相同的方式).

indexPath.row只是一个Int,表示tableView的选定行.

然后devCourses [indexPath.row]是该索引的元组.例如,如果行为0,那么元组将是(“iOS App Dev with Swift Essential Training”,“Simon Allardice”).

最后,您可以通过将它们声明为元组并使用元组初始化它们来同时初始化两个变量.例如:

let (a,b) = (3,4)

创建两个Int常量a和b,并将3分配给a和4分配给b.

所以这个语句是从整数indexPath.row指示的数组中检索元组并创建两个变量,为第一个变量courseTitle分配元组中第一个值的值,并为第二个变量赋值courseAuthor中第二个值的值.元组.

Swift 3的更新

NSIndexPath仍然存在于Foundation中,但是当在Swift 3中使用indexPaths时,现在使用了新类型的IndexPath.这种类型是一种结构.

您仍然可以使用以下更新语法在Swift 3中创建NSIndexPath,但无法更改属性:

var ip = NSIndexPath(row: 0,section: 0)

ip.row = 5    // Cannot assign to property: 'row' is a get-only property

但您应该使用新的IndexPath结构.

IndexPath使用类似的语法创建:

var ip2 = IndexPath(row: 0,section: 0)

ip2.row = 5   // this works

您可以通过使用as转换来在IndexPath和NSIndexPath之间进行转换:

let ip = NSIndexPath(row: 0,section: 0)
let ip2 = ip as IndexPath     // convert to IndexPath
let ip3 = ip2 as NSIndexPath  // convert back to NSIndexPath

所有使用indexPaths的Cocoa和Cocoa Touch API都已转换为在Swift中使用IndexPath.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读