Swift tableView cell doesnt have a webView member - swift

I made a custom tableview cell with basic style. I put webView inside content and I'm populating my cell with string on lable (works fine ), but I can't acess webView.
There is no function on "cell.webView", so i can't populate my view. How can I populate a cell with webView on custom cell?

This is how you can do this:
Step 1 : Create a custom cell by subclassing UITableViewCell
Step 2 : Add a property webView in it.
class MyTableViewCell: UITableViewCell {
var webView = UIWebView()
var companyName = UILabel()
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code. Add details based on your needs.
webView.frame = CGRectMake(0, 0, 200, 20)
contentView.addSubview(webView)
}
}
Step 3 : Use MyTableViewCell to supply cells for your table view. Simply call cell.webView now.

Related

Is it possible to create a supplementary side view for a UICollectionView on the side instead of as a header or footer?

I'm trying to create a setup where I have a UICollectionView with a header on top, and then another supplementary view on the side. I can't just place the supplementary view outside of the collectionView because it shares space with the collectionView cells inside of the collectionView and I also want it to scroll with it. Here's how it should look:
I've been thinking, and one solution I can think of is making a big collectionView, with the supplementary side view and the view to the right as cells of it, with the header as its header. However, I've heard it's not great to put a collectionView inside of a collectionView, especially if the scrolling directions are the same, and I also want to dynamically load more cells as I scroll, so in that case I would have to dynamically keep resizing the cell.
Is this configuration possible, in an elegant manner? Thanks!
It is possible to achieve that . There is a easy way that set Frame of Supplementary View and add UIEdgeInsets in the left of SectionInset .
Code as follow :
flowLayout = new UICollectionViewFlowLayout (){
HeaderReferenceSize = new CGSize(50, 50),
SectionInset = new UIEdgeInsets (20,65,20,20),
ScrollDirection = UICollectionViewScrollDirection.Vertical,
MinimumInteritemSpacing = 10, // minimum spacing between cells
MinimumLineSpacing = 10 // minimum spacing between rows if ScrollDirection is Vertical or between columns if Horizontal
};
The custom UICollectionReusableView class :
public class Header : UICollectionReusableView
{
UILabel label;
public string Text {
get {
return label.Text;
}
set {
label.Text = value;
SetNeedsDisplay ();
}
}
[Export ("initWithFrame:")]
public Header (CGRect frame) : base (frame)
{
BackgroundColor = UIColor.Red;
label = new UILabel (){Frame = new CGRect (0,80,50,500), BackgroundColor = UIColor.Yellow};
label.Lines = 5;
AddSubview(label);
}
}
The effect :
Above code based on this official sample to modify .

Dynamic Tableview inside UItableViewCell

I want to implement subTableView with dynamic height inside the UITableViewCell, where as the subTableView has cells with dynamic UITableViewCell.Please share your thoughts on this if anybody have implemented this scenario.
-UItableview(Parent)
-UItableviewCell
-UITableView(child)
-UItableviewCell(dynamic content).
First Approach: Create a subclass for child tableView and override intrinsicContentSize.
class MyOwnTableView: UITableView {
override var intrinsicContentSize: CGSize {
self.layoutIfNeeded()
return self.contentSize
}
override var contentSize: CGSize {
didSet{
self.invalidateIntrinsicContentSize()
}
}
override func reloadData() {
super.reloadData()
self.invalidateIntrinsicContentSize()
}
}
2. In Interface builder change the class of your child tableView to MyOwnTableView (subclass UItableView).
Set automatic row height for both the parent and child table view.
tableView.estimatedRowHeight = 60.0;
tableView.rowHeight = UITableViewAutomaticDimension;
Second Approach:
1. Create a height constraint with any value for child tableView and conect an IBOutlet that sets the child tableView height.
2. Set the height constraint's constant to tableView.contentSize.height
self.tableViewHeight.constant = self.tableView.contentSize.height
Use Auto Layout when creating your UItableviewCell(dynamic content)..
Set the UITableView(child) rowHeight to UITableViewAutomaticDimension.
Set the estimatedRowHeight of UITableView(child).
you can see this website:
https://www.raywenderlich.com/129059/self-sizing-table-view-cells

Custom UITableViewCell programmatically with SWIFT

Attached you may find a picture of (left) my attempt to create a similar (right) UITableView. they look like buttons but they're only cells that can be clicked on as a tableview cell.
I have tried many different things including adding a container window inside a custom cell, adding a UIImage inside the custom cell but I just can't replicate these cells!
I have tried using a custom cell class, I have tried doing it through IB and I for the crazyiness of me, cannot recreate it.
Would anyone be able to give me a hint on how to create the (inside cell) text-bounding box/square? with the different background colour lighting?
If this can easily be done with IB I'd rather do it this way, but if you have a sample customcell class that I can take a look at that'd be greatly appreciated too!
Thank you for taking the time to look at my question.
I have made a sample for you close to your requirement. Have a look
https://github.com/RajanMaheshwari/CustomTableCell
I would like to do this using a UITableView.
My approach will be taking a custom cell and add a UIView with some constraints from left, right, up and down.
Also I will provide the same background color to UITableView, UIView which is the superview and the cell content view and also make the separator of UITableView as None and Selection of TableCell as None so that the UI looks like
Next after applying every constraint and making a CustomCell and making IBOutlets we will jump to code.
I will do all the shadow and outlining in Custom Cell's awakeFromNib method
This will be my CustomTableViewCell class
class CustomTableViewCell: UITableViewCell {
#IBOutlet weak var labelBackgroundView: UIView!
#IBOutlet weak var cellLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
labelBackgroundView.layer.borderWidth = 0.5
labelBackgroundView.layer.borderColor = UIColor.lightGrayColor().CGColor
labelBackgroundView.layer.shadowColor = UIColor.lightGrayColor().CGColor
labelBackgroundView.layer.shadowOpacity = 0.8
labelBackgroundView.layer.shadowRadius = 5.0
labelBackgroundView.layer.shadowOffset = CGSizeMake(0.0, 2.0)
labelBackgroundView.layer.masksToBounds = false;
}
I have two outlets.
One is the label in which you will be displaying the name.
Other is the outer view which you want to display with some outlining and shadow.
The ViewController code will be:
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
var array = [String]()
#IBOutlet weak var myTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
array = ["Wealth","Health","Esteem","Relationship"]
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("CustomTableViewCell") as! CustomTableViewCell
cell.cellLabel.text = array[indexPath.row]
cell.labelBackgroundView.tag = indexPath.row
cell.labelBackgroundView.userInteractionEnabled = true
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(cellViewTapped))
cell.labelBackgroundView.addGestureRecognizer(tapGesture)
return cell
}
func cellViewTapped(sender:UITapGestureRecognizer) {
let view = sender.view
let index = view?.tag
print(index!)
}
}
Here I have not used didSelectIndex of UITableViewDelegate as I only want the tap on the Outlining LabelBackgroundView and not on complete cell.
So the final outcome is like this
I think you are on the right path. I did something similar in a project. I created a subclass of UIView (too add a shadow to the view) and added a view with this type inside the cell.
class ShadowedView: UIView {
override func awakeFromNib() {
layer.shadowColor = UIColor(red: 157.0/255.0, green: 157.0/255.0, blue: 157.0/255.0, alpha: 0.5).CGColor
layer.shadowOpacity = 0.8
layer.shadowRadius = 5.0
layer.shadowOffset = CGSizeMake(0.0, 2.0)
}
}
Don't forget to add some constraints to the view inside the cell.
You can arrange your collection view layout in "Size insepector"
And customise your image in the cell.

TableView Cells / Visual Format Language / Keeps adding same constraints?

Creating UITableViewCells including elements programmatically seems to be working except for the constraints. Or maybe it is. Everytime the cell reappears the same constraints will be added to the element. I'll have 20 of the same constraints added to a uiLabel after scrolling tableView. Here is my sublcass (condensed):
class TimeSlotCell: UITableViewCell {
let lblTime = UILabel()
var viewCons = [String:AnyObject]()
var previousCageView:UIView!
var myConstraints = [NSLayoutConstraint]()
func configureCell(row:Int,cageCount:Int) {
lblTime.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(lblTime)
placeConstraint("H:|-5-[time\(row)]", view:"time\(row)")
placeConstraint("V:|-17-[time\(row)]", view: "time\(row)
}
func placeConstraint(format:String, view:String) {
viewCons[view] = lblTime
let navViewConstraint = NSLayoutConstraint.constraintsWithVisualFormat(format, options: [], metrics: nil, views: viewCons)
myConstraints += navViewConstraint
NSLayoutConstraint.activateConstraints(myConstraints)
}
And in my VC i'm calling the configureCell func within my cellForRowAtIndexPath.
Is there a way to check if an existing constraint exists? Is there a way to make sure only one of my constraints can be added? Or is there a person out there with a much better solution? Thanks in advance.
There is a dataSource for your tableView, when you configure your cell, you send the cell model to your cell. Whenever the dataSource changes, the cell model sent to the cell will change as well. So what you need to do is move most of 'addSubview' to the cell`s initialization. If you have to do cell.contentView.addSubview with the cell model. You have to judge if the cell model you send to the cell is the same as the former one. If it is equal, do nothing, else reset the view config.

Swift adaptive cell height in static tableview

I am creating a static tableview to as my page will have a mixture of text and images in different rows.
Here is an example. This was created in obj c webView, but i want to use Swift and be native, no webview
Here is the code in my tableViewController:
import UIKit
class AdaptiveRowTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
self .tableView.rowHeight = UITableViewAutomaticDimension
self .tableView.estimatedRowHeight = 44
}
override func viewDidAppear(animated: Bool ) {
tableView.reloadData()
}
}
I have set the row height to 0.
I have tested with a label and an image. The label works fine, but the image's row is not adjusting correctly.
Here is the result.
Any ideas on how to fix the image row height?
There is a nice apple video where you can find how to create adaptive cell height from minute 24.
You have to create constraints between the text and image and the cell and create relationships between them. Basically set a top and bottom constraint between text and cell and create a height relationship between text and image.