简体   繁体   中英

Swift Modifying Tableviews with arrays

Hi I'm nearly done with my personal project but came up stuck when I was trying modify my table view with my array. Here's my function that I'm using; it takes the data I have and puts it respectively into the annotation name, coordinates of the annotation and then goes into the table view, which is all contained within a custom map callout.

The way I set it up is that using the i value it counts off and matches the name and coordinates perfectly, and if I put i into "index" for the item list it does exactly that by counting them off.

My issue is that I want to be able to display multiple lines of information within the tableview, which is the use of the "for _ in 0...1". However, when I do this in conjunction with the index=i I get this screenshot .

Would anyone know how I would go about getting 2 lines of info into each pool's tableview? The end result would have the correct pool name and location(which is already done) and then the correct corresponding address and indoor/outdoor description.

My code is below, any help would be greatly appreciated I've searched everywhere but can't find anything useful that wouldn't require me to redo everything.

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)
    }
} 

Your code is confusing because you have a constant wishlist and later a local variable wishlist which are different objects.

You are appending the same item twice because you are not using the current loop index but the outmost index i

Maybe you mean in the first iteration items 0 and 1 are used, in the second items 2 and 3 and so on.

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

which requires that the constant wishlist contains at least kPoolsWishListManagerNumberOfPools * 2 items otherwise you get an out-of-range crash.


Basically if the data is static anyway I recommend to extend the pool initializer

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

and populate the array this more efficient way

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",
            ... ]

or save the data in a JSON file on disk and decode the JSON into the Pool instances.

Your wishlist values are not grouped consistently. Try changing wishlist to this:

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"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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