how to organize tableview cells - ios

I have two different kinds of tableview cells in one table view. The first cell prints out original comments to a post, the second cell prints out comments to another comment. Currently, the tableview prints out all the correct cells in no particular order. However, I want to print the cells in a particular order. I want the cells that contain comments to another comment to appear below the comment it is being commented on.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Configure the cell...
let cell = tableView.dequeueReusableCell(withIdentifier: "Main", for: indexPath) as! PostTableViewCell
//Configure the cell
cell.PostView.layer.cornerRadius = 5
cell.PostView.layer.masksToBounds = false
cell.PostView.layer.shadowColor = UIColor.black.withAlphaComponent(0.4).cgColor
cell.PostView.layer.shadowOffset = CGSize(width: 0, height: 0)
cell.PostView.layer.shadowOpacity = 0.9
let post = Comments[indexPath.row] as! [String: AnyObject]
let commentname = post["author"] as? String
sendAuthor = post["author"] as? String
cell.CommentersName.setTitle(commentname, for: .normal)
if let seconds = post["pub_time"] as? Double {
let timeStampDate = NSDate(timeIntervalSince1970: seconds/1000)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMM d, yyyy"
let formating = timeStampDate as Date
cell.CommentTime.text = dateFormatter.string(from: formating)
}
cell.comment.text = post["content"] as? String
textViewDidChange(cell.comment)
cell.comment.frame.size.width = 344
cell.comment.sizeToFit()
cell.comment.clipsToBounds = true
cell.REply.frame.origin.y = cell.comment.frame.maxY + 10
cell.report.frame.origin.y = cell.comment.frame.maxY + 10
cell.Likes.frame.origin.y = cell.comment.frame.maxY + 10
cell.LikesNumber.frame.origin.y = cell.comment.frame.maxY + 10
cell.PostView.frame.size.height = cell.comment.frame.maxY + 50
TableView.rowHeight = cell.PostView.frame.size.height + 20
cell.CommentersName.sizeToFit()
cell.pole.frame.origin.x = cell.CommentersName.frame.maxX + 5
cell.CommentTime.frame.origin.x = cell.pole.frame.maxX + 5
let numLikes = post["num_likes"] as? NSNumber
cell.LikesNumber.text = String(describing: numLikes!)
replyId = post["id"] as? String
let replyTo = post["reply_to"] as? String
let postID = post["post_id"] as? String
if replyTo == postID {
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "Reply", for: indexPath) as! RepliesTableViewCell
cell.ReplyCustomCell.layer.cornerRadius = 5
cell.ReplyCustomCell.layer.masksToBounds = false
cell.ReplyCustomCell.layer.shadowColor = UIColor.black.withAlphaComponent(0.4).cgColor
cell.ReplyCustomCell.layer.shadowOffset = CGSize(width: 0, height: 0)
cell.ReplyCustomCell.layer.shadowOpacity = 0.9
let post = Comments[indexPath.row] as! [String: AnyObject]
cell.ReplyText.text = post["content"] as? String
let commentname = post["author"] as? String
cell.author.setTitle(commentname, for: .normal)
if let seconds = post["pub_time"] as? Double {
let timeStampDate = NSDate(timeIntervalSince1970: seconds/1000)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMM d, yyyy"
let formating = timeStampDate as Date
cell.time.text = dateFormatter.string(from: formating)
}
let numLikes = post["num_likes"] as? NSNumber
cell.num_likes.text = String(describing: numLikes!)
textViewDidChange(cell.ReplyText)
cell.ReplyText.frame.size.width = 232
cell.ReplyText.sizeToFit()
cell.ReplyText.clipsToBounds = true
cell.author.sizeToFit()
cell.pole.frame.origin.x = cell.author.frame.maxX + 5
cell.time.frame.origin.x = cell.pole.frame.maxX + 5
cell.Likes.frame.origin.y = cell.ReplyText.frame.maxY + 10
cell.num_likes.frame.origin.y = cell.ReplyText.frame.maxY + 10
cell.reportButton.frame.origin.y = cell.ReplyText.frame.maxY + 10
cell.replyButton.frame.origin.y = cell.ReplyText.frame.maxY + 10
cell.ReplyCustomCell.frame.size.height = cell.ReplyText.frame.maxY + 50
TableView.rowHeight = cell.ReplyCustomCell.frame.size.height + 20
return cell
}
cell.checkfornightmode()
return cell
}
The comments that are associated with each other have the same "id", how will I organize the cells so that the comments of a main comment will be listed under the original comment. Thank you

You can create one Comment custom object class which will hold an array of sub comments and the main comment to arrange or manage your data structure properly. After that you can use it properly with your table view cell.
Okay so for example you can have the below data structure.
Create one Comment class:
class Comment {
comment_id
content
post_id
reply_to
}
Now create one more class for your table view:
class CommentTableDataModel {
var mainComment: Comment // Of type Comment class
var replies: [Comment] // Array of type Comment class for sub comments
}
So now just iterate through your firebase Comments array and prepare an array list of type 'CommentTableDataModel' objects as a datasource for your table. So finally you will have an array of type object 'CommentTableDataModel' and each object of type 'CommentTableDataModel' contains the main comment info as well as the list of replies info with that, with this you can manage your data.

Related

Multiple collectionviews in one Viewcontroller causes Index out of range error

I try to use three collectionviews in one Viewcontroller. I parse the data like the following method shows:
At the bottom i add the data depending on the position to the right list (this part works)
func getEventData(eventIDs: [String], plz: String, positiona: Int){
for eventId in eventIDs {
let ref = Database.database().reference().child("Events").child(plz).child(eventId)
ref.observe(.value, with: { snapshot in
let item = snapshot.value as? [String: AnyObject]
let eventName = item?["name"] as! String
let date = item?["date"] as! String
let lat = item?["lat"] as! String
let lng = item?["lng"] as! String
let infos = item?["additionalInfos"] as! String
let position = item?["position"] as! String
let ts = item?["ts"] as! Int
let createdBy = item?["createdBy"] as! String
let timestamp = NSDate().timeIntervalSince1970
if (ts > Int(timestamp)) {
let eo = EventObject(eventID: eventId, eventName:
eventName, info: infos, createdBy: createdBy, date: date, lat: lat, lng: lng, position: position, ts: ts)
if positiona == 0{
self.acceptedEvents.append(eo)
self.acceptedEventscv.reloadData()
}else if positiona == 1{
self.myEvents.append(eo)
self.myEventscv.reloadData()
}else if positiona == 2{
self.storedEvents.append(eo)
self.storedEventscv.reloadData()
}
}
})
}
}
In my NumbersofItemsInSection method i did the following which works as well:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView == self.acceptedEventscv{
return acceptedEvents.count
}else if collectionView == self.storedEventscv{
return storedEvents.count
}else if collectionView == self.myEventscv{
return myEvents.count
}else{
return 0
}
}
and in my CellForRowAtItem method i tried the following
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == self.acceptedEventscv{
let cell =
collectionView.dequeueReusableCell(withReuseIdentifier:
"acceptedEventsCell", for: indexPath) as!
acceptedEventsCollectionViewCell
let eo = acceptedEvents[indexPath.row]
cell.eventName.text = eo.eventName
let items = eo.date!.components(separatedBy: " ")//Here replase
space with your value and result is Array.
//Direct line of code
//let items = "This is my String".components(separatedBy: " ")
let date = items[0]
let time = items[1]
cell.date.text = date
cell.time.text = time
getUserNameAge(label: cell.usernameAge)
return cell
}else {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "savedEventsCell", for: indexPath) as! savedEventsCollectionViewCell
let eo = acceptedEvents[indexPath.row]
cell.eventName.text = eo.eventName
let items = eo.date!.components(separatedBy: " ")//Here replase space with your value and result is Array.
//Direct line of code
//let items = "This is my String".components(separatedBy: " ")
let date = items[0]
let time = items[1]
cell.date.text = date
cell.time.text = time
getUserNameAge(label: cell.usernameAge)
return cell
}
}
The problem is if i have more items in the seccond CollectionView, i always get this error:
Thread 1: Fatal error: Index out of range
at this line of code:
let eo = acceptedEvents[indexPath.row]
cellForItemAt also must be like
if collectionView == self.acceptedEventscv{
let item = acceptedEvents[indexPath.row]
----
return cell
}else if collectionView == self.storedEventscv{
let item = storedEvents[indexPath.row]
----
return cell
}else {
let item = myEvents[indexPath.row]
----
return cell
}
what happens now in your case is that you access the same array acceptedEvents in both cases where the returned count in numberOfItemsInSection may be different

get data from firebase children

I have two custom cells in one table view.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Configure the cell...
if (indexPath.row == 0) {
let cell = tableView.dequeueReusableCell(withIdentifier: "Main", for: indexPath) as! PostTableViewCell
//Configure the cell
cell.PostView.layer.cornerRadius = 5
cell.PostView.layer.masksToBounds = false
cell.PostView.layer.shadowColor = UIColor.black.withAlphaComponent(0.4).cgColor
cell.PostView.layer.shadowOffset = CGSize(width: 0, height: 0)
cell.PostView.layer.shadowOpacity = 0.9
let post = Comments[indexPath.row] as! [String: AnyObject]
let commentname = post["author"] as? String
sendAuthor = post["author"] as? String
cell.CommentersName.setTitle(commentname, for: .normal)
if let seconds = post["pub_time"] as? Double {
let timeStampDate = NSDate(timeIntervalSince1970: seconds/1000)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMM d, yyyy"
let formating = timeStampDate as Date
cell.CommentTime.text = dateFormatter.string(from: formating)
}
cell.comment.text = post["content"] as? String
textViewDidChange(cell.comment)
cell.comment.frame.size.width = 344
cell.comment.sizeToFit()
cell.comment.clipsToBounds = true
cell.REply.frame.origin.y = cell.comment.frame.maxY + 10
cell.PostView.frame.size.height = cell.comment.frame.maxY + 50
TableView.rowHeight = cell.PostView.frame.size.height + 20
cell.LikesNumber.text = post["num_likes"] as? String
replyId = post["id"] as? String
cell.checkfornightmode()
return cell
}
else{
let cell = tableView.dequeueReusableCell(withIdentifier: "Reply", for: indexPath) as! RepliesTableViewCell
cell.ReplyCustomCell.layer.cornerRadius = 5
cell.ReplyCustomCell.layer.masksToBounds = false
cell.ReplyCustomCell.layer.shadowColor = UIColor.black.withAlphaComponent(0.4).cgColor
cell.ReplyCustomCell.layer.shadowOffset = CGSize(width: 0, height: 0)
cell.ReplyCustomCell.layer.shadowOpacity = 0.9
let post = Comments[indexPath.row] as! [String: AnyObject]
let posttest = post["id"] as? String
let replyRef = Database.database().reference().child("main").child("posts").child(postID!).child("comments").child(posttest!).child("comments")
replyRef.observeSingleEvent(of: .value, with: { (snapshot:DataSnapshot) in
if let postsDictionary = snapshot .value as? [String: AnyObject] {
for testingkey in postsDictionary.keys {
Database.database().reference().child("main").child("posts").child(self.postID!).child("comments").child(posttest!).child("comments").child(testingkey).observeSingleEvent(of: .value, with: { (snapshot) in
let value = snapshot.value as? NSDictionary
let content : String? = value?["content"] as? String ?? ""
cell.ReplyText.text = content!
})
}
}
})
TableView.rowHeight = 150.0
return cell
}
}
The first cell with the identifier main is supposed to print out all the intial comments for a certain post. The second cell with the identifier is supposed to print out the comments to main comments. Based on this code, I am only getting the last comment of the main post and the last comment to the main posts.
This is what the json looks like
First thing is to separate the firebase call into a separate function and in that function populate a dictionary with posts as key and comments as value
for example like this
var postComments: [String: [String]] = [:] // post as key and string array as comments
In firebase database call populate this with snapshot.
After the data call but within the firebase Database call back use this function to reload data
DispatchQueue.main.async{
tableView.reloadData()
}
Set the cell with postComments array.

Screen Hangs and terminated

Screen gets hanged and terminated. I have trace down that because of my while loop ,screens hanged and app terminated.
While loop runs only of 10 times. when I reopen my application my Data is got saved.
How to null or deallocate the UITableViewcell
let itemcell:TableViewCell = self.tableView.cellForRow(at: item) as! TableViewCell
Since self.tableView.dequeueReusableCell(withIdentifier: "listdetails", for: item) returning empty value .I have to use cellforRow.
Please provide input on this.
#IBAction func saveButton(_ sender: UIBarButtonItem) {
let ndx = IndexPath(row:0, section: 0)
var counter:Int = 0
let cell = self.tableView.cellForRow(at: ndx) as! TableViewCell
let locationNameCell = self.tableView.cellForRow(at: IndexPath(row:1, section: 0)) as! TableViewCell
let shoppingDetails = ShoppingDetails(context:managedContext)
let storeName = cell.storeName.text!
let storeFlag = validateShoppingList(storeName: storeName)
if storeFlag == true {
let shoppingDetails = ShoppingDetails(context:managedContext)
var listDetails :ListDetails!
while counter < sectionRowCount {
let item = IndexPath(row:counter, section: 1)
var itemcell = self.tableView.cellForRow(at: item) as! TableViewCell
let list = shoppingDetails.shoppingToList?.mutableCopy() as! NSMutableSet
listDetails = ListDetails(context:managedContext)
let listItem = itemcell.listItem.text!.trimmingCharacters(in: .whitespaces)
listDetails.listItem = listItem
var qty = itemcell.qtytextfield.text!
if qty.isEmpty {
qty = "0"
}
var units = itemcell.unitstextfield.text!
if units.isEmpty {
units = ""
}
listDetails.qty = Int64(qty)!
listDetails.units = itemcell.unitstextfield.text!
listDetails.isChecked = false
list.add(listDetails)
shoppingDetails.addToShoppingToList(list)
counter = counter + 1
list.removeAllObjects()
}
shoppingDetails.storeName = storeName
if locationNameCell.locationName.text != nil {
shoppingDetails.location = locationNameCell.locationName.text!
}
shoppingDetails.initialLetter = (cell.storeName.text!).first?.description
let seqNo:Int = ShoppingDetails.getSeqNo(managedObjectContext: managedContext) + 1
shoppingDetails.seqNo = Int32(seqNo)
coreData.saveContext()
let vc = storyboard!.instantiateViewController(withIdentifier: "StoreDisplayController") as! StoreDisplayController
vc.managedContext = managedContext
vc.coreData = coreData
vc.storeName = cell.storeName.text!
// managedContext.delete(shoppingDetails)
// managedContext.delete(listDetails)
// self.present(vc, animated:true, completion:nil)
// self.navigationController?.popToViewController(vc, animated: true)
self.navigationController?.pushViewController(vc, animated: true)
}
enter image description here
Save your data in this method as the user will scroll to fill all content
func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
// ...
//fill model array parameters from cell properties before data lost
var itemcell = self.tableView.cellForRow(at: indexPath) as! TableViewCell
let list = shoppingDetails.shoppingToList?.mutableCopy() as! NSMutableSet
listDetails = ListDetails(context:managedContext)
let listItem = itemcell.listItem.text!.trimmingCharacters(in: .whitespaces)
listDetails.listItem = listItem
var qty = itemcell.qtytextfield.text!
if qty.isEmpty {
qty = "0"
}
var units = itemcell.unitstextfield.text!
if units.isEmpty {
units = ""
}
listDetails.qty = Int64(qty)!
listDetails.units = itemcell.unitstextfield.text!
listDetails.isChecked = false
list.add(listDetails)
shoppingDetails.addToShoppingToList(list)
}
then when save button is clicked check that model array for empty item and scroll to that indexpath to fill

UITableViewCell content disappears after scrolling

I have a UITableView which gets its data from Parse. It is basically like a feed that has message posts, UIMapView and UIImages. Some posts have only text however, so I've made up 4 different cell types. For all the different combinations of posts.
Text Alone
Image with Text
Image and Map with Text
Map with Text
After that, in my cellForRowAtIndexPath function, I retrieve all the messages from Parse.
This is my function:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell:MessageTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as? MessageTableViewCell
let cell2:FullTableViewCell! = tableView.dequeueReusableCellWithIdentifier("AllCell", forIndexPath: indexPath) as? FullTableViewCell
let cell3:ImageTableViewCell! = tableView.dequeueReusableCellWithIdentifier("ImageCell", forIndexPath: indexPath) as? ImageTableViewCell
let cell4:MapTableViewCell! = tableView.dequeueReusableCellWithIdentifier("MapCell", forIndexPath: indexPath) as? MapTableViewCell
var typeOfCell = ""
if(self.feedData.count > indexPath.row){
if let message:PFObject = (self.feedData[indexPath.row] as? PFObject)!{
long = message.objectForKey("LocationLongitude") as! Double
lat = message.objectForKey("LocationLatitude") as! Double
if let userImageFile = message["Photos"]{
userImageFile.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
let imageData = imageData
let image = UIImage(data:imageData!)
if(self.long != 0 || self.lat != 0){
//ALL INCLUDED
cell2.imgView.image = image
cell2.txtMessage.text = message.objectForKey("Message") as? String
cell2.txtMessage.font = UIFont(name: "Baskerville", size: 18)
let dataFormatter:NSDateFormatter = NSDateFormatter()
dataFormatter.dateFormat = "H:mm - MM-dd-yyyy"
cell2.lblDate.text = dataFormatter.stringFromDate(message.createdAt!)
typeOfCell = "FullCell"
}else{
//ONLY IMAGE
cell3.imgView.image = image
cell3.txtMessage.text = message.objectForKey("Message") as? String
cell3.txtMessage.font = UIFont(name: "Baskerville", size: 18)
let dataFormatter:NSDateFormatter = NSDateFormatter()
dataFormatter.dateFormat = "H:mm - MM-dd-yyyy"
cell3.lblDate.text = dataFormatter.stringFromDate(message.createdAt!)
//typeOfCell = "ImageCell"
}
}
}
}else{
if(self.long != 0 || self.lat != 0){
//MAPVIEW
cell4.txtMessage.text = message.objectForKey("Message") as? String
cell4.txtMessage.font = UIFont(name: "Baskerville", size: 18)
let dataFormatter:NSDateFormatter = NSDateFormatter()
dataFormatter.dateFormat = "H:mm - MM-dd-yyyy"
cell4.lblDate.text = dataFormatter.stringFromDate(message.createdAt!)
typeOfCell = "MapCell"
}else{
//ONLY TEXT
cell.txtMessage.text = message.objectForKey("Message") as? String
cell.txtMessage.font = UIFont(name: "Baskerville", size: 18)
let dataFormatter:NSDateFormatter = NSDateFormatter()
dataFormatter.dateFormat = "H:mm - MM-dd-yyyy"
cell.lblDate.text = dataFormatter.stringFromDate(message.createdAt!)
typeOfCell = "Cell"
}
}
}
}
if(typeOfCell == "FullCell"){
self.tableView.rowHeight = 861.00
print("FullCell")
return cell2
}else if(typeOfCell == "Cell"){
print("Cell")
self.tableView.rowHeight = 133.00
return cell
}else if(typeOfCell == "MapCell"){
print("MapCell")
self.tableView.rowHeight = 269.00
return cell4
}else{
print("ImageCell")
self.tableView.rowHeight = 725.00
return cell3
}
}
So far so good. When I run my app however, Only the first cell displays an image (if it has one) and the rest do not. Also, when I try to scroll in my TableView, all my images disappear. So basically I can only view two types of cells. Either type 1 or type 4 in the previously mentioned cell types. I've been stuck on this issue for hours and I can't seem to figure it out. Any help is greatly appreciated.
While using tableViews You should use Else conditions for contents of cell as it reuses cell so when you scroll up down it reuses cell. That causes the problem.

Firebase function freezes app in xcode7, but works in xcode6.4

For some reason this code works perfectly in xcode6.4, but when switching to xcode7 it freezes the app.
What I am trying to do is pull the post information on a user's feed and display it on a tableview. I am able to pull the information from Firebase, but the app freezes before it displays on the tableview.
EDIT: The tableview works when I do not have any constraints or autolayout. It seems to not work when I try to have dynamic cell heights.
func getRadarData() {
let url = "https://(insert appname).firebaseio.com/users/" + currentUser + "/postsReceived/"
let targetRef = Firebase(url: url)
targetRef.observeEventType(.ChildAdded, withBlock: {
snapshot in
print("child")
if let found = self.posts.map({ $0.key }).indexOf(snapshot.key) {
let obj = self.posts[found]
print(obj)
print(found)
self.posts.removeAtIndex(found)
}
let postsUrl = "https://(insert appname).firebaseio.com/posts/" + snapshot.key
let postsRef = Firebase(url: postsUrl)
var updatedAt = snapshot.value["updatedAt"] as? NSTimeInterval
var endAt = snapshot.value["endAt"] as? NSTimeInterval
if updatedAt == nil {
updatedAt = 0
}
if endAt == nil {
endAt = 0
}
postsRef.observeSingleEventOfType(.Value, withBlock: { snapshot in
if let key = snapshot.key
{if let content = snapshot.value["content"] as? String {
if let creator = snapshot.value["creator"] as? String {
if let createdAt = snapshot.value["createdAt"] as? NSTimeInterval {
let userurl = "https://(insert appname).firebaseio.com/users/" + (creator)
let userRef = Firebase(url: userurl)
userRef.observeSingleEventOfType(.Value, withBlock: { snapshot in
if let username = snapshot.value["username"] as? String {
let updatedDate = NSDate(timeIntervalSince1970: (updatedAt!/1000))
let createdDate = NSDate(timeIntervalSince1970: (createdAt/1000))
let endedDate = NSDate(timeIntervalSince1970: (endAt!))
let post = Post(content: content, creator: creator, key: key, createdAt: updatedDate, name: username, joined: true, messageCount: 0, endAt: endedDate)
self.posts.append(post)
// Sort posts in descending order
self.posts.sortInPlace({ $0.createdAt.compare($1.createdAt) == .OrderedDescending })
self.tableView.reloadData()
}
})
}
}
}
}
})
})
}
Here is my code for my tableview where I used autolayout on the textView and nameLabel
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: RadarTableViewCell = tableView.dequeueReusableCellWithIdentifier("radarCell", forIndexPath: indexPath) as! RadarTableViewCell
let creator: (String) = posts[indexPath.row].creator
let key = posts[indexPath.row].key
let radarContent: (AnyObject) = posts[indexPath.row].content
cell.textView.selectable = false
cell.textView.text = radarContent as? String
cell.textView.userInteractionEnabled = false
cell.textView.selectable = true
let radarCreator: (AnyObject) = posts[indexPath.row].name
cell.nameLabel.text = radarCreator as? String
return cell
The issue was that I had initial text in my textView. I deleted it on my Storyboard and my app works now.
Found the solution here: Why does a previously working Xcode project hang up in Xcode 7 when presenting a new UITableviewController Subclass?

Resources