以下是我从API获取的示例JSON数据。我想使用swift在tableviewcell中显示"ArticleTitle"
和"ImageURL"
。
"ArticleTitles":[{"ArticleID":"872",
"ArticleTitle":"IS 'NOTHlNGNESS' AN EXPERIENCE ?",
"Author":"Asampoorna",
"ImageURL":"http://motherofall.org/sites/default/files/blog/Ignorance.png"
},{..........}]
答案 0 :(得分:0)
在响应中,您将获得结果数组。然后在你尝试使用数组的东西之后,这是错误的。获得结果数组后,所有处理都应在cellForRowAtIndexPath方法中完成。
将结果数组作为全局而不是取tableData和imageData。
if let url = NSURL(string: "http://motherofall.org/sites/default/files/blog/Ignorance.png") {
if let data = NSData(contentsOfURL: url){
cell.imageView.contentMode = UIViewContentMode.ScaleAspectFit
cell.imageView.image = UIImage(data: data)
}
}
按照this帖子在tableViewCell中异步加载图片。
使用以下代码将图像加载到tableview单元格内的图像视图中。
class Offers
{
/**
* @var string
* @Assert\Length(
* min = 5,
* max = 64,
* minMessage = "El nombre de la oferta debe de tener al menos {{ limit }} caracteres.",
* maxMessage = "El nombre de la oferta no puede superar los {{ limit }} caracteres."
* )
*/
private $name;
/**
* @var string
*/
private $description;
/**
* @var string
* @Assert\Url(message = "La url '{{ value }}' no es válida")
*/
private $url;
/**
* @var string
*/
private $img;
/**
* @var \DateTime
*/
private $dateFrom;
/**
* @var \DateTime
*/
private $dateTo;
/**
* @var \DateTime
*/
private $registered;
/**
* @var boolean
*/
private $active;
/**
* @var integer
*/
private $availableFor;
/**
* @var integer
*/
private $id;
/**
* @var \Doctrine\Common\Collections\Collection
*/
private $idState;
/**
* Constructor
*/
public function __construct()
{
$this->idState = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Set name
*
* @param string $name
* @return Offers
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set description
*
* @param string $description
* @return Offers
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set url
*
* @param string $url
* @return Offers
*/
public function setUrl($url)
{
$this->url = $url;
return $this;
}
/**
* Get url
*
* @return string
*/
public function getUrl()
{
return $this->url;
}
/**
* Set img
*
* @param string $img
*/
public function setImg($img = null)
{
if ( $img != null)
$this->img = $img;
}
/**
* Get img
*
* @return string
*/
public function getImg()
{
return $this->img;
}
/**
* Set dateFrom
*
* @param \DateTime $dateFrom
* @return Offers
*/
public function setDateFrom($dateFrom)
{
$this->dateFrom = $dateFrom;
return $this;
}
/**
* Get dateFrom
*
* @return \DateTime
*/
public function getDateFrom()
{
return $this->dateFrom;
}
/**
* Set dateTo
*
* @param \DateTime $dateTo
* @return Offers
*/
public function setDateTo($dateTo)
{
$this->dateTo = $dateTo;
return $this;
}
/**
* Get dateTo
*
* @return \DateTime
*/
public function getDateTo()
{
return $this->dateTo;
}
/**
* Set registered
*
* @param \DateTime $registered
* @return Offers
*/
public function setRegistered($registered)
{
$this->registered = $registered;
return $this;
}
/**
* Get registered
*
* @return \DateTime
*/
public function getRegistered()
{
return $this->registered;
}
/**
* Set active
*
* @param boolean $active
* @return Offers
*/
public function setActive($active)
{
$this->active = $active;
return $this;
}
/**
* Get active
*
* @return boolean
*/
public function getActive()
{
return $this->active;
}
/**
* Set availableFor
*
* @param integer $availableFor
* @return Offers
*/
public function setAvailableFor($availableFor)
{
$this->availableFor = $availableFor;
return $this;
}
/**
* Get availableFor
*
* @return integer
*/
public function getAvailableFor()
{
return $this->availableFor;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Add idState
*
* @param \Consolidador\PanelBundle\Entity\States $idState
* @return Offers
*/
public function addIdState(\Consolidador\PanelBundle\Entity\States $idState)
{
$this->idState[] = $idState;
return $this;
}
/**
* Remove idState
*
* @param \Consolidador\PanelBundle\Entity\States $idState
*/
public function removeIdState(\Consolidador\PanelBundle\Entity\States $idState)
{
$this->idState->removeElement($idState);
}
/**
* Get idState
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getIdState()
{
return $this->idState;
}
/**
* Set idState
* @param \Consolidador\PanelBundle\Entity\States $idState
*/
public function setIdState($idState)
{
$this->idState[] = $idState;
}
/**
* Cadena de texto a devolver.
* @return string
*/
public function __toString()
{
return $this->name;
}