gpt4 book ai didi

ios - 从 Parse 下载后使用图像或文本进行排序

转载 作者:行者123 更新时间:2023-11-30 13:37:17 27 4
gpt4 key购买 nike

我有一个 tableView,每个单元格中包含三个图像,其中显示我的帖子类中的所有图像。所以我下载所有图像,然后在将它们放入单元格中时将它们除以 3!因此,下载时它不会在每个单元格中显示相同的图像 3 次...

有点令人困惑,但如果我向您展示我的代码,您可能会理解它如何更好地工作!

但无论如何,我现在想做的事情是相同的,但带有文本,因此在单元格中,根据下载的内容,它将正常显示图像,或者如果它们是文本,它将显示文本而不是图片!!

// HERE'S HOW I SORT THE 3 IMAGES WHEN DOWNLOADED (CellForRowAtIndexPath)

if post.dataType == 1
{
for imageView in cell.threeImages {
imageView.image = UIImage(named: "ImagePlaceHolder")

if counter < posts.count{
imageView.hidden = false
let imagePost = posts[counter++].image
imagePost!.getDataInBackgroundWithBlock { (data:NSData?, error:NSError?) -> Void in
if error == nil {
if let imageData = data {
imageView.image = UIImage(data: imageData)
}

}
}
} else {
imageView.hidden = true
}



// AND HERE'S HOW I SORT THEM / DIVIDE THEM BY 3 SO THEY'RE NOT ALL THE SAME FOR EACH ROW.

func roundUp(value: Double) -> Int {
if value == Double(Int(value)) {
return Int(value)
} else if value < 0 {
return Int(value)
} else {
return Int(value) + 1
}
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{

return = roundUp(Double(posts.count) / 3.0)


}

这是模拟器上现在的样子,下载所有图像后,它还从解析中下载 1 个文本列:[![在此处输入图像描述][1]][1]

现在,当我在这里尝试此代码后:

//IMAGES

if post.dataType == 1
{
//Images
for imageView in cell.threeImages {
imageView.image = UIImage(named: "ImagePlaceHolder")

if counter < posts.count{

imageView.hidden = false

let imagePost = posts[counter++].image
imagePost!.getDataInBackgroundWithBlock { (data:NSData?, error:NSError?) -> Void in
if error == nil {
if let imageData = data {
imageView.image = UIImage(data: imageData)

//TEXT
for textHide in cell.threeLabels{
textHide.hidden = true
}}}} } else {
imageView.hidden = true
}

我得到的结果几乎就在那里:

[![在此处输入图像描述][2]][2]

但我实际上追求的是文本可以与图像混合在一起的东西,如下所示:

[![在此处输入图像描述][3]][3]

我有一种感觉,这可能与 DataType 代码有关,因为它允许所有图像或所有文本......所以这里也是我的结构的代码!

truct PostStruct {
var dataType : Int = 0 // 0 for text, 1 for picture
var date : NSDate
var username : NSString
var text : NSString?
var title : NSString?
var image : PFFile?
var uuid : NSString
var profileImage : PFFile
var caption : NSString?


init (dataTypeInit: Int, dateInit : NSDate, usernameInit: String, textInit: String, titleInit: String, uuidInit: String,profileImageInit: PFFile) {
dataType = dataTypeInit
date = dateInit
username = usernameInit
text = textInit
title = titleInit
uuid = uuidInit
profileImage = profileImageInit
}

init (dataTypeInit: Int, dateInit : NSDate, usernameInit: String, imageInit: PFFile, uuidInit: String, profileImageInit: PFFile, captionInit: String) {
dataType = dataTypeInit
date = dateInit
username = usernameInit
image = imageInit
uuid = uuidInit
profileImage = profileImageInit
caption = captionInit
}
}


var posts : [PostStruct] = []

更新

                for imageView in cell.threeImages {
imageView.image = UIImage(named: "ImagePlaceHolder")
for textView in cell.threeLabels {


let rowData = tableData[indexPath.row]

for indexInRow in 0...2
{
if indexInRow > rowData.posts.count
{
textView.hidden = true
imageView.hidden = true
// there are less posts for this row than the current index - so hide both the image and the text
// hide the imageView at position indexInRow
// hide the text view (label) at position indexInRow
}
else
{
let postt = rowData.posts[indexInRow]
// there is a post - check to see if it's text or an image
if postt.dataType == 1
// image type (replace this with an enumerator at some point)
{
postt.image!.getDataInBackgroundWithBlock { (data:NSData?, error:NSError?) -> Void in
if error == nil {
if let imageData = data {
imageView.image = UIImage(data: imageData)

imageView.hidden = false
textView.hidden = true

}

}
}


// display the image in image at position indexInRow
// hide the text view (label) at position indexInRow
}
else
{
imageView.hidden = true
textView.hidden = false
textView.text = postt.text as? String
// hide the imageView at position indexInRow
// display the text view (label) at position indexInRow
}
}
}}
}

预先感谢您的帮助!

最佳答案

我认为你必须继续除以三,这让事情变得过于复杂 - 为什么不为已经分成三行的表格 View 创建数据?

您可以拥有像这样的 rowData 结构

struct rowData
{
var posts : [PostStruct] = []
}

并将其存储在像这样的类级变量中

var tableData : [rowData] = []

然后在加载数据时将其解析为该结构

// create an array of tableData, 
// where each element is of type rowData
// which has up to 3 `post` entries
// each of which is either an image or a text post
// the tableView methods are then based on tableData
var indexInRow = 0
for post in posts
{
if indexInRow == 0
{
// create a new rowData object
tableData.append(rowData())
}
tableData[tableData.count - 1].posts.append(post)

// using the modulo operator like this will return the remainder of dividing by 3, so it will always be 0, 1, 2
indexInRow = (indexInRow + 1) % 3
}

然后你的 tableView 方法看起来像这样

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let rowData = tableData[indexPath.row]

for indexInRow in 0...2
{
if indexInRow > rowData.posts.count
{
// there are less posts for this row than the current index - so hide both the image and the text
// hide the imageView at position indexInRow
// hide the text view (label) at position indexInRow
}
else
{
// there is a post - check to see if it's text or an image

// UPDATE
let post = rowData.posts[indexInRow]
// UPDATE

if post.dataType == 0 // image type (replace this with an enumerator at some point)
{
// display the image in image at position indexInRow
// hide the text view (label) at position indexInRow
}
else
{
// hide the imageView at position indexInRow
// display the text view (label) at position indexInRow
}
}
}
return UITableViewCell()
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return tableData.count
}

关于ios - 从 Parse 下载后使用图像或文本进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35940310/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com