-2

Hi am developing a app using swift 2.2 am getting a data from the server in that data am getting multiple images which is seperated by commas eg..,

{
    "success": 1,
    "error": 0,
    "message": [{
        "title": "aver monitor",
        "image": "3037-1486200291.jpg,5056_25326756.jpg",
        "views": "0",
        "price": "12000",
        "postdate": "2017-02-04 14:54:51",
        "type": "0"
    }]
}

in that image value am getting images i need to pass that images to the collection view.

the images am getting from the server that do not exceeds more than 5 and i want to display only 5 so i wrote the function as:

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 5
}

i can able to seperate the value by using commas seperated by string(",") if i pass the value am getting same image for 5 times.i need to load different image in all the cell..if there is only one image means i need to load that particular image in the first cell others cells should load static image that may be any image..

code i tried:

let json = JSON(data: data!)

let imgString = json["message"]["image"].stringvalue

let myData = imgString.componentsseperatedbystring(",")

my datasource method:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("editPendingCell", forIndexPath: indexPath) as! pendingEditCollectionViewCell

    cell.myImage.sd_setImageWithURL(NSURL(string:pendingImageString))

    return cell
}
Rob
  • 415,655
  • 72
  • 787
  • 1,044
user7333282
  • 101
  • 1
  • 2
  • 15
  • Unrelated, that JSON is not well designed. `image` should be an array of strings, not a string. Do you have any option to fix that, rather than program around the poor design? Also, I don't know how `json["message"]["image"]` worked, because `message` is an array of dictionaries. It should have been `json["message"][0]["image"]`. – Rob Feb 04 '17 at 13:14
  • The problem rests in `pendingImageString`. But you don't show us where you set that, so it's a bit hard to tell you how to fix it other than noting that you probably should be referencing the array confusingly named `myData`, not `pendingImageString`. – Rob Feb 04 '17 at 13:16
  • Also unrelated, but classes should start with uppercase letters, e.g. `PendingEditCollectionViewCell`, not `pendingEditCollectionViewCell`. – Rob Feb 04 '17 at 13:19
  • every thing was typed by me....so its just an example,,,help me to do that one – user7333282 Feb 04 '17 at 13:27
  • Your question doesn't make much sense. It looks like you're getting JSON from the server. You shouldn't try to parse that as a string. Instead you should use NSJSONSerialization to convert the JSON data to objects, and then traverse your objects to get to filename(s) you need. – Duncan C Feb 04 '17 at 16:13
  • @DuncanC - The problem is that `image` of his JSON appears to be a single string (which is a horrible design), not an array of strings like it should be. His problem isn't converting the JSON to objects (he's apparently using SwiftyJSON for that). Nor does it seem to be the parsing of that `image` string into array of separate strings (because he'd doing that with `myData`). But his problem appears to be with the use of `pendingImageString` (which he refuses to share with us) rather than saving the array of the poorly named `myData` into some property and referencing that. – Rob Feb 04 '17 at 20:12

1 Answers1

0

Ok. Based on comments, it seems you have been able to extract the "image" key/value pair from your JSON.

Your code appears correct. You have a line

   let myData = imgString.componentsseperatedbystring(",")

That code should create an array myData that would contain your image names. It would be better named imageNames.

You should make that an instance variable, and then index into it to fetch the image for each of your cells.

If you're having problems then you need to show the end-to-end-code that builds your array of filenames, along with information about where you store that information.

Uma Madhavi
  • 4,851
  • 5
  • 38
  • 73
Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • i have done that process what you mentioned above now i need to insert new cell..for eg.. i need to display only five cell if imageName has two image means that successfully passed into my collectionview now i want to display static images to the remaining cell....how to do that???? – user7333282 Feb 06 '17 at 05:10
  • What do you mean "...display static images to the remaining cell..." What static images and what "remaining" cell? Your description is vague and unclear. – Duncan C Feb 06 '17 at 11:42