ios - 使用数组快速修改 Tableview

标签 ios arrays swift uitableview

嗨,我的个人项目即将完成,但当我尝试使用数组修改 TableView 时,我遇到了困难。 这是我正在使用的功能;它获取我拥有的数据并将其分别放入注释名称、注释坐标中,然后进入表格 View ,这全部包含在自定义 map 标注中。

我设置它的方式是使用 i 值进行计数并完美匹配名称和坐标,如果我将 i 放入项目列表的“索引”中,它会通过对它们进行计数来完成此操作。

我的问题是我希望能够在tableview内显示多行信息,这就是“for _ in 0...1”的使用。但是,当我与 index=i 结合执行此操作时,我得到这个 screenshot .

有人知道我如何将两行信息放入每个池的表格 View 中吗?最终结果将具有正确的泳池名称和位置(已经完成),然后是正确的相应地址和室内/室外描述。

我的代码如下,任何帮助将不胜感激,我到处搜索,但找不到任何有用的东西,不需要我重做所有事情。

func populatePoolsList() {
    let names = ["BSC Waltham", "Weston Golf Club", "BSC Wellesley", "Life Time Natick"]

    let coordinates = [
        CLLocationCoordinate2D(latitude: 42.400366, longitude: -71.279059),
        CLLocationCoordinate2D(latitude: 42.361025, longitude: -71.287842),
        CLLocationCoordinate2D(latitude: 42.348945, longitude: -71.228759),
        CLLocationCoordinate2D(latitude: 42.312967, longitude: -71.394940),
        ]
    let wishlist = ["444 Winter St, Indoor Pool", "275 Meadowbrook Rd", "Outdoor Pool", "Someplace in Wellesley", "Both Infoor and Outdoor Pools", "a place in Natick", "Either pools"]
     let items = ["444 Winter St, Indoor Pool", "275 Meadowbrook Rd", "Outdoor Pool", "Someplace in Wellesley", "Both Infoor and Outdoor Pools", "a place in Natick", "Either pools"]

    pools = []
    for i in 0..<kPoolsWishListManagerNumberOfPools {
        let name = names[i]
        let avatar = UIImage(named: "outdoorpool 2")!
        var wishlist = [String]()
        for _ in 0...1 {
            let index = i
            wishlist.append(items[index])
        }

        let pool = Pool(name: name, avatar: avatar)
        pool.wishList = wishlist
        pool.location = coordinates[i]
        pools.append(pool)
    }
} 

最佳答案

您的代码很困惑,因为您有一个常量 wishlist 和一个局部变量 wishlist,它们是不同的对象。

您将同一项目追加两次,因为您没有使用当前循环索引,而是使用最外层索引i

也许您的意思是在第一次迭代中使用项目 0 和 1,在第二次迭代中使用项目 2 和 3,依此类推。

let index = i * 2
wishlist = [items[index], items[index+1]]

这要求常量 wishlist 至少包含 kPoolsWishListManagerNumberOfPools * 2 项,否则会发生超出范围的崩溃。

<小时/>

基本上,如果数据是静态的,我建议扩展池初始值设定项

init(name: String, avatar: UIImage, coordinate: CLLocationCoordinate2D, wishlist : [String])

并以更有效的方式填充数组

func populatePoolsList() {
   pools = [Pool(name: "BSC Waltham", avatar: UIImage(named: "outdoorpool 2")!, coordinate: CLLocationCoordinate2D(latitude: 42.400366, longitude: -71.279059), wishlist : "444 Winter St, Indoor Pool", "275 Meadowbrook Rd")
            Pool(name: "Weston Golf Club", avatar: UIImage(named: "outdoorpool 3")!, coordinate: CLLocationCoordinate2D(latitude: 42.361025, longitude: -71.287842), wishlist : "275 Meadowbrook Rd", "Outdoor Pool",
            ... ]

或者将数据保存在磁盘上的 JSON 文件中,并将 JSON 解码到 Pool 实例中。

关于ios - 使用数组快速修改 Tableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51918661/

相关文章:

ios - 为 iTunes Connect 存档时的 Cocoa 框架问题

ios - 是否可以使用 SpriteKit 创建复杂的平台游戏?

java - 将数组中的多个字符串打印为单个字符串

ios - 在图像上绘制的文本延伸出图像

javascript - 当数组达到高于 x 的数字时换行

java - Java中的getView和Beacon(Estimote)问题

swift - 为什么我收到错误 "Type of expression is ambiguous without more context"?

ios - mainRunloop 上的 NSURLConnection 不工作

ios - 收到 EXC_BAD_ACCESS 信号

ios - 如何设置 PFQuery 以使用 queryForTable 获取具有给定角色的所有用户?