I have a function called loadPosts that returns an array of Int values. Upon running it is used within the UITableView which has a function called setCell. Only the first item in the array is being used and then it repeats that value for the length of the array.
UPDATE 2: Here are the parameters within the hhmessages array: 1. senderusername 2. recipient 3. message text 4. ava image
UPDATED: now includes additional code in loadPosts function
func loadPosts()->[Int] {
let me = user!["username"] as! String
let uuid = messages["uuid"] as! String
let url = URL(string: "http://localhost/message.php")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
let body = "username=\(me)&uuid=\(uuid)"
request.httpBody = body.data(using: String.Encoding.utf8)
URLSession.shared.dataTask(with: request) { data, response, error in
DispatchQueue.main.async(execute: {
if error == nil {
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
self.hhmessages.removeAll(keepingCapacity: false)
self.tableView.reloadData()
// declare new parseJSON to store json
guard let parseJSON = json else {
print("Error while parsing")
return
}
guard let messages = parseJSON["messages"] as? [AnyObject] else {
print("Error while parseJSONing")
return
}
self.hhmessages = messages
//print(self.hhmessages)
for i in 0 ..< self.hhmessages.count {
if me == self.hhmessages[i]["senderusername"]!! as! String {
self.incoming = [0]
}
if me == self.hhmessages[i]["recipient"]!! as! String {
self.incoming = [1]
}
}
self.tableView.reloadData()
return [Int()]
}
// UITableView
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ConversationCell
func setCell(incoming: [Int]) {
var layoutAttribute: NSLayoutAttribute
var layoutConstant: CGFloat
for i in 0 ..< self.incoming.count {
if (self.incoming[i] == 1) {
cell.bubbleImageView.image=#imageLiteral(resourceName: "chat_bubble_received")
cell.messageLbl.textColor = UIColor.black
layoutAttribute = .left
layoutConstant = 10
cell.contentView.addConstraint(NSLayoutConstraint(item: cell.bubbleImageView, attribute: layoutAttribute, relatedBy: .equal, toItem: cell.contentView, attribute: layoutAttribute, multiplier: 1, constant: layoutConstant))
}
if (self.incoming[i] == 0) {
cell.bubbleImageView.image = #imageLiteral(resourceName: "chat_bubble_sent")
cell.messageLbl.textColor = UIColor.white
layoutAttribute = .right
layoutConstant = -10
cell.contentView.addConstraint(NSLayoutConstraint(item: cell.bubbleImageView, attribute: layoutAttribute, relatedBy: .equal, toItem: cell.contentView, attribute: layoutAttribute, multiplier: 1, constant: layoutConstant))
}
}
}
// get main queue to this block of code to communicate back
DispatchQueue.main.async {
tableView.transform = CGAffineTransform(rotationAngle: -CGFloat.pi)
cell.transform = CGAffineTransform(rotationAngle: CGFloat.pi)
setCell(incoming: self.incoming)
}
return cell
}
returninteger array is correct ? do you mind to post an example of yourhhmessages?hhmessagesto understand the structure