I am creating a UITabelViewCell looks like the the feed in Facebook which contains feed content, images, comments and so on.
To simplify the problem, lets consider only aUILabelon the top and aUIImageView` below the lable. All the constraints are set properly and the height of the cell is caculated dynamicly according to the constraints.
The challenge I face is that sometimes the feed only have content and sometimes it contains both content and images. I do not want to create two UITabelViewCell, cause it will make it harder to maintain.
The method I use now is remove the UIImageView if I find no image in the data, but I have to add it back when necessary, and all these can be done at "tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)". But I don`t think it is a good solution, can some one show me some good points, thanks!
Related
I want to learn and implement the most suitable/simple solution to display dynamic data (JSON) in three lines and an active (clickable) download icon. Screenshot is attached
I would be glad to get your ideas and advice!
Thanks
UITableView is the way to go, as you can reuse a custom cell you define. And to add to that, UITableView's cells are loaded lazily and are reused. You can use a UIScrollView but if you have a lot of rows it can horde your memory real fast. In your case, a UIScrollView can be used as well, but if you plan on expanding, UITableViews would be the choice. All in all, go with a UITableView + a custom UITableViewCell
If you have array of data like this, the best way is using UITableView class. With UITableview, you can custom your UITableViewCell on the fly
I am creating my iOS app, and i during that , now i have to build scrollable screen where there is image view, labels, collection view, Reviews etc (Prototype Image is attached). I am wondering is it possible to make that type of screen with UITable view using multiple Nib files? (If it is possible, kindly guide me as how i should use multiple Nib files with multiple arrays). And , if it is not possible, how else i can go for it? Kindly guide me, and sorry if thats a duplicate because i have searched but i was not able to find any good answer.
There are several approaches that you can do.
If you want to use UItableview, then you need to create multiple
UITableviewCells, where each cells is based on the section of the design. Then you can load it by using array, as you logic stated above. For example indexpath[0] you'll load the cell with the picture(lets call this pictureCell), then indexpath[1] you'll load descriptionCell, and so on.
The other approach is using Scrollview, with several UIViews on a vertical stackview. And then you'll create a UIView Subclass based on the section of the design. This is usually my go to solution.
I'm not sure what "array" is refering to here, but by the look of your screenshot, I would go with a StackView inside a UIScrollView, or something like that. No need for a UITableView if you don't have a repeting pattern, you can just stack multiple views in a scroll view.
I hope it's clear enough.
I have UITableView and UITableViewCell.
I get data from API. Some items have a link, others have not.
If the item has not to link I wand to hide button with a book icon.
When I use this method (look below) button is hidden right, but then when the tableview reuse this cell icon with a book does not come back. How I can fix it?
var addButtonTrailingConstraint = openPdfButton.widthAnchor.constraint(equalToConstant: 0)
if link == nil{
NSLayoutConstraint.activate([addButtonTrailingConstraint])
}else{
NSLayoutConstraint.deactivate([addButtonTrailingConstraint])
}
}
This is kinda hard to answer without more code / knowledge of your constraint setup.
But I can give you 2 tips how to solve this issue by taking another approach:
1. Approach: Use UIStackView to manage your buttons:
Remove your buttons and replace them with a UIStackView. Then in code, where you config your cell (set text, title, ...) you first remove all Buttons from the UIStackView (you can do this easily with stackView.removeAllArrangedSubviews(), this is needed because the cells are getting reused and you don't want to add more and more buttons every time the cell is getting displayed.
After that, add the buttons you need in this cell (e.g.: like this: stackView.addArrangedSubview(button)).
This approach has the benefit that it is very dynamic, you can add as many different buttons as you wish without having to modify your code.
But since you need to create new buttons all the time it is not the most performance efficient solution.
2. Approach: Use 2 different UITableViewCell classes:
Make 2 different UITableViewCells, one with one button and a second one with 2 buttons. You can also inherit one from another to reduce duplicate code.
Then in tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) check which one of the 2 cell classes you need, create the right one and set its members (text, title, ...).
This approach is less flexible but more performance efficient in comparison to the 1. approach.
I use both approaches in production and they are working quite nicely :)
You need
if link == nil {
openPdfButton.widthAnchor.constraint(equalToConstant: 0).isActive = true
} else {
openPdfButton.constraints.forEach {
openPdfButton.removeConstraint($0)
}
}
Problem
I have a UICollectionView initialised from code and I register a custom class for its cells. The custom class has a few UIStackViews to lay out a bunch of UILabels and UIViews.
One of the arranged subviews should contain a variable number of UIViews. To make this less abstract, let’s think of them as “seats" that will display faces of users attending this event (can be up to four, can be none)
Solutions that come to mind
(I’m a novice)
Solution 1
Have the custom class lay out all the "seat" UIViews so I can treat them as placeholders;
Have the UICollectionView’s cellForItemAt method iterate over the data source and unhide the “seats” that have been filled
Solution 2
Make sure that the UIStackView in the custom class can be referenced from UICollectionView’s cellForItemAt method
From cellForItemAt use addArrangedSubview to add all the needed “seats”
Solution 3
Register 5 different classes (zero “seats” to “four seats”)
Dequeue each one as dictated by the data source
This one seems to me like the least dignified, but might be the one with least performance penalty
I'm perfectly aware that there could be more, and better, solutions. So, what would be the optimal way of implementing this variable number of UIViews, and why?
EDIT:
While I think that the layout of the cell here is irrelevant, it's been requested that I post it
so here it is...
I propose Solution 3, as it will be much more performant.
Also, I have two advices:
Avoid using UIStackView, as it has slow layout performance (it is based on AutoLayout). Also, try to avoid AutoLayout in UITableViewCell and UICollectionViewCell for better performance. Here are some of the most popular and powerful open source layout libraries PinLayout, LayoutKit.
You can write base UICollectionViewCell, move all of the relevant logic there, and then just make all the needed subclasses with minimal logic overriding.
I would like to know how to handle, for my iOS app, the following situation:
I have to deal with something like this (basicly this is an article with comments, received from a server):
=- Text and Images -=
=- UIWebView -=
=- List of Comments -=
Now, I came up with two solutions:
Have the content above the list of comments wrapped in a UIScrollView, and create Views for each and everyone of the comments (don't know many of them could be), and
Make the list of comments a UITableView, and the above content its Header.
Which of these (or possibly another if you have any recommandations) should I choose? It may look not very important, but I would like to know this, so I could use the idea in further developing.
I would use a UITableView with a custom UITableViewCell wich holds the comments.
It will definitely have a better performance the UITableView than the UIScrollView since the UITableView re-uses the cells.
Using a UITableView you will just have to worry about customising the cells for the comments.
Otherwise if you want to use the UIScrollView in case you have a lot of comments you will have to create manually a way to reuse them which is what the UITableView does.
If you want something like facebook, so that the context + comments both are movable,then go with option 2.
But, if you want content always at the top, go with option 1.
Indeed, UITableView is a subclass of UIScrollView.
And If you think you might have many comments like (50+),
If you use scrollView, then you should have to supply scrollView with those number of UIView objects.
But, if you use tableView, it perfectly reused already created views.So evenhough you have 1000+ comments, it just uses 5 UIView objects