I am new to swift, using swift 3, I built tableview that reads group of images and I want to have it as below:
when user select an image in the tableview I display it in an UIImageView and when Deselect an image he already selected in the tableview to be removed from the UIImageView ( Multiple Selection is enabled). My code below is working for one image selection only, however if multiple images are selected and then deselected the last selected one only removed from the UIImageview. So my question is how can I remove the UIImageview that belongs to the deselected record in the UItableview
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
image_view.image = UIImage(data: Saveddata[indexPath.row].photo as! Data)
//create image view programmatically
currentImageView = UIImageView(frame: CGRect(x: 130 + (i * 10), y: 50 + (i * 5), width: 50, height: 100))
currentImageView.contentMode = .scaleAspectFit
currentImageView.image = image_view.image
view.addSubview(currentImageView as UIView)
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
currentImageView.removeFromSuperview()
}
You could use tag for each UIImageView based on the indexPath.row so you could refer by tag.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let image = UIImage(data: Saveddata[indexPath.row].photo as! Data)
//create image view programmatically
let imageView = UIImageView(frame: CGRect(x: 130 + (i * 10), y: 50 + (i * 5), width: 50, height: 100))
imageView.contentMode = .scaleAspectFit
imageView.tag = 100 + indexPath.row
imageView.image = image
view.addSubview(imageView as UIView)
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
// get imageView by its tag
let deselectedImageView = view.viewWithTag(100 + indexPath.row) as! UIImageView
deselectedImageView.removeFromSuperview()
}
If you want to update your cell appearance when selected/deselected, you should override the method setSelected(_:animated:) inside your UITableViewCell subclass instead of trying to update it directly from your UITableViewDelegate method:
override func setSelected(_ selected: Bool, animated: Bool) {
// Do whatever you want depending on the selected value
}
Related
I am trying to add a UIView to an existing IBOutlet in a tableview cell. However this does not appear until I have scrolled away from the cell and scroll back to it.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let stockCell = tableView.dequeueReusableCell(withIdentifier: "stockCell", for: indexPath) as! customStockCell
let progressBar = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
progressBar.backgroundColor = .green
stockCell.progressContainerView.addSubview(progressBar)
return cell
}
class customStockCell: UITableViewCell {
#IBOutlet var progressContainerView: UIView!
}
Using contentView instead of containerView works fine for me.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let stockCell = tableView.dequeueReusableCell(withIdentifier: "stockCell", for: indexPath)
let progressBar = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
progressBar.backgroundColor = .green
stockCell.containerView.addSubview(progressBar)
return cell
}
You can also create a custom cell class and add this logic inside its' initialiser.
Alternatively, if the above solution doesn't work, you can add this logic inside willDisplayCell:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let progressBar = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
progressBar.backgroundColor = .green
cell.containerView.addSubview(progressBar)
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am trying to create tableView cells and customize the layout. But it seems anything I add inside my cells get stuck being the same size height and width as the cell itself. How can I make it to where I can design my cells however I like? i.e, make my images a specific size with margins and paddings. Even my text height is being stretched out to the cells height. I added the borders so that it's visible for what I mean. The height of my label should be just the size of the text itself but it is still stretching.
I am stuck on tableview cells and want to design my cell layout fully programmatically without storyboarding. I've been trying to teach myself all day and just keep going in circles.
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
let tableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
self.tableView.delegate = self
self.tableView.dataSource = self
self.view.addSubview(self.tableView)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
tableView.frame = view.bounds
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 200
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 8
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = "Here is some text"
cell.textLabel?.layer.cornerRadius = 10
cell.textLabel?.layer.borderWidth = 5
cell.textLabel?.layer.borderColor = UIColor.black.cgColor
cell.imageView?.image = UIImage(named: "spiderman")!
cell.imageView?.layer.cornerRadius = 10
cell.imageView?.layer.borderWidth = 5
cell.imageView?.layer.borderColor = UIColor.black.cgColor
return cell
}
}
Thats because you are using UITableViewCell's default textLabel
If you open a storyboard and play around with cell with 'basic' style, you will see that the text is always aligned to center.
I dont think you can adjust height or position of these two predefined properties (.textLabel and .imageView) directly.
Try to create imageView and label manually, and dont use cell.textLabel directly.
Example:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let label = UITextView(frame: CGRect(x: 200, y: 0, width: 500, height: 50))
label.text = "Here is some text"
label.layer.cornerRadius = 10
label.layer.borderWidth = 5
label.layer.borderColor = UIColor.black.cgColor
cell.addSubview(label)
let image = UIImage(named: "spiderman")!
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
imageView.image = image
imageView.layer.cornerRadius = 10
imageView.layer.borderWidth = 5
imageView.layer.borderColor = UIColor.black.cgColor
cell.addSubview(imageView);
return cell
}
I am trying to constraint elements (UIView or UITextfield) in a TableViewCell. If i am using a frame like
UITextField(frame: CGRect(x: 150, y: 0, width: 500, height: 110))
everything works fine.
But i am trying to use autoresize and somehow my UITextfield added as SubView in my Cell don't have the same ancestor. The error message is: "they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies?"
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var Testtext: UITextField{
let test = UITextField(frame: CGRect(x: 150, y: 0, width: 500, height: 110))
test.textColor = .red
test.font = .boldSystemFont(ofSize: 22)
test.text = "Hello World"
return test
}
var cell:UITableViewCell {
let createdCell = UITableViewCell()
createdCell.contentView.addSubview(Testtext)
// Testtext.translatesAutoresizingMaskIntoConstraints = false
// NSLayoutConstraint.activate([
// Testtext.topAnchor.constraint(equalTo: createdCell.topAnchor),
// Testtext.bottomAnchor.constraint(equalTo: createdCell.bottomAnchor),
// Testtext.trailingAnchor.constraint(equalTo: createdCell.trailingAnchor),
// Testtext.leadingAnchor.constraint(equalTo: createdCell.leadingAnchor)
// ])
return createdCell
}
BTW: I don't want to create a Cellclass, because i need to adjust the number of Textfields and UIViews depending on the input arrayscount.
You need
var cell:UITableViewCell {
let createdCell = UITableViewCell()
createdCell.contentView.addSubview(Testtext)
Testtext.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
Testtext.topAnchor.constraint(equalTo: createdCell.contentView.topAnchor),
Testtext.bottomAnchor.constraint(equalTo: createdCell.contentView.bottomAnchor),
Testtext.trailingAnchor.constraint(equalTo: createdCell.contentView.trailingAnchor),
Testtext.leadingAnchor.constraint(equalTo: createdCell.contentView.leadingAnchor)
])
return createdCell
}
but this isn't the correct way you need to dequeueResuableCell inside cellForRowAt to generate the cells instead of creating vars
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CustomCell
return cell
}
How do I resize the imageView?
Xcode says initialisation of imageView was never used.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
let city = nicosiaPlaces [indexPath.row]
cell?.textLabel?.text = city
let imageView = CGSize(width: 100, height: 100)
cell?.imageView?.image = UIImage(named: city)
return cell!
}
You just create a new CGSize every time when the method is called, you should change the size in your cell class instead. And if you're using storyboard with AutoLayout, you should change the size in storyboard.
I guess your question has some flaws inside.
Assuming that you need to resize an Imageview I'm posting small Snippet for you.
Never initialize any new component inside func tableView(cellForRowAt:IndexPath)
beacuse every time it is going to create a new Instance until they are properly disposed.
You have taken reference as let imageView = CGSize(--,--) but you are again calling the instance from Cell Method.
Don't do that.
override func awakeFromNib(){
let imageView = UIImageView(CGRectMake(0,0,150,150))
imageView.contentMode = .scaleAspectFill
contentView.addSubView(imageView)
}
Now In the Class method get reference of the ImageView and resize if you want.
overrride func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.deququeResuableCell(withIdentifier: "cellID",forIndexPath: indexPath) as! yourCustomCellClassName
cell.imageView?.image = UIImage(named: "yourImage")
// If you need to resize the Image make changes in Frame of Image or ContentMode of the Image.
return cell
}
I have this function:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!
let label = UILabel(frame: CGRect(x: 100, y: 14, width: 400, height: 30))
label.text = "\(data[indexPath.row])"
label.tag = indexPath.row
cell.contentView.addSubview(label)
return cell
}
Well, with this function I'm adding a list of rows dynamically.
Why dynamically? Because, the number of columns depends of the data.
Don't take focus on that.
The list has 244 elements.
The result is displayed ok, but once I started scrolling, I get this:
How I can add elements dynamically without get this error?
Cells get reused. You keep adding more and more labels to each cell.
The proper solution is to create a custom cell class that contains the desired label. Then simply set that label's text in cellForRowAt. Don't create and add subviews in cellForRowAt.
But keep in mind that you may just want to use the provided textLabel property of UITableViewCell. No need for a custom cell or your own label.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!
cell.textLabel?.text = "\(data[indexPath.row])"
return cell
}
I found a solution, hope this works to you!!!
You have to add to your cellForRowAt this code:
for item in cell.contentView.subviews
{
item.removeFromSuperview()
}
Just after to get the cell.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!
for item in cell.contentView.subviews
{
item.removeFromSuperview()
}
let label = UILabel(frame: CGRect(x: 100, y: 14, width: 300, height: 30))
label.text = "\(data[indexPath.row])"
label.tag = indexPath.row
let btn = UIButton(type: UIButtonType.custom) as UIButton
btn.backgroundColor = UIColor.red
btn.setTitle("boton", for: .normal)
btn.frame = CGRect(x:0, y:5, width: 80, height:40)
//btn.addTarget(self, action: "buttonPressed:", for: UIControlEvents.touchUpInside)
btn.tag = indexPath.row
cell.contentView.addSubview(btn)
cell.contentView.addSubview(label)
cell.tag = indexPath.row
return cell
}
UITableview create 10 cell at scroll down recreate cell view
You can using
cell.contentView.removeFromSuperview() before cell.contentView.addSubview(label) or using cell.textLabel?.text
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!
cell.contentView.removeFromSuperview()
let label = UILabel(frame: CGRect(x: 100, y: 14, width: 400, height: 30))
label.text = "\(data[indexPath.row])"
label.tag = indexPath.row
cell.contentView.addSubview(label)
return cell
}
Note This solution will delete All subview in cell