0

am new to swift code

enter image description here

this is my json after parsing I have to display on tableview, I can get the date, details,eventid properly but am not able to get the "eventImage" inside banner image I can try but am not getting that pls help me
this is my code
json calling function

if errorCode == "0" {


                            if let Media_list = jsonData["events"] as? NSArray {


                                for i in 0 ..< Media_list.count {

                                    if let MediaEventData = Media_list[i] as? NSDictionary {




       =====>>     Hear the problem    let imageURL = self.url+"/images/" + String(describing: MediaEventData["bannerImage"]!)

                                            self.Mediainfo.append(MediaEvent(
                                            eventId: MediaEventData["eventId"]as?String,
                                            date: MediaEventData["date"]as?String,
                                            eventname: MediaEventData["eventName"]as?String,
                                            bannerImages: imageURL


                                           )
                                        )
                                    }

                                }
                                self.do_table_refresh()
                            }

my tablview code

  override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Media", for: indexPath)as! MediaCustomTableViewCell

       let  row = indexPath.row


        let media = Mediainfo[row] as MediaEvent

        cell.DisplayDate.text = media.date

        cell.DisplayName.text = media.eventName


         cell.DisplayImage.image = UIImage(named: "profile_pic")
         cell.DisplayImage.downloadImageFrom(link: media.bannerImages!, contentMode: .scaleAspectFit)


        // Configure the cell...

        return cell
    }

how can display the "banner image" tableview

3
  • 1
    As you are new to Swift please first learn the naming convention that variable names start with a lowercase letter and to use Swift native types Array and Dictionary rather than typeless NS... Foundation types. Commented Aug 7, 2017 at 11:39
  • I had date with multiple images I want to display ex : 8/08/2017 had two images but I want display that day first image in tableview how can I do Commented Aug 8, 2017 at 7:27
  • if can try bellow code it's display the hole images of that day pls help me Commented Aug 8, 2017 at 7:28

2 Answers 2

1

I believe problem is with the way you are accessing the banner image I think it should be like this

MediaEventData["eventImages"][i]["bannerImage"]

where i is index of your eventImages array

In your code you can do this:

 var imageUrl: String = ""
if let Media_list = jsonData["events"] as? [Any] {


    for i in 0 ..< Media_list.count {

        if let MediaEventData = Media_list[i] as? [String: Any] {

            let eventImages = MediaEventData["eventImages"] as! [[String: Any]]
                if eventImages.count > 0 {
                   let bannerImage = eventImages[0]["bannerImage"] as? String

                   imageUrl = self.url+"/images/" + String(describing: bannerImage!)

                }



            self.Mediainfo.append(MediaEvent(
                eventId: MediaEventData["eventId"]as?String,
                date: MediaEventData["date"]as?String,
                eventname: MediaEventData["eventName"]as?String,
                bannerImages: imageURL


                )
            )



    }        
   }         

}
Sign up to request clarification or add additional context in comments.

11 Comments

This code will cause a lot of 'Any' has no subscript members errors.
I had date with multiple images I want to display ex : 8/08/2017 had two images but I want display that day first image in tableview how can I do
@naga I updated my answer to display first image please check, if you have any questions please ask
@user1000 I tried above code after I am getting this error message type 'any' has no subscript members
I think you need to downcast as Array , I updated my answer please check
|
0

Working fine to me

 if errorCode == "0" {


                            if let Media_list = jsonData["events"] as? NSArray {

                                self.Mediainfo.removeAll()


                                for i in 0 ..< Media_list.count {

                                    if let MediaEventData = Media_list[i] as? NSDictionary {

                                        if let image_list = MediaEventData["eventImages"] as? NSArray
                                         {

                                         for i in 0 ..< image_list.count
                                         {

                                            if let mydata = image_list[i] as? NSDictionary

                                            {
                                                let datavalue = mydata["bannerImage"]as? String
                                                self.imageurl = self.url+"/images/events/" + datavalue!

                                            self.Mediainfo.append(MediaEvent(
                                            eventId: MediaEventData["eventId"]as?String,
                                            date: MediaEventData["date"]as?String,
                                            eventname: MediaEventData["eventName"]as?String,
                                            bannerImages: self.imageurl
                                            ))

                                            }

                                            }
                                        }
                                    }


                                }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.