Here I attached the gif file. Refer this
In my tableview I have a custom xib cell with autolayout. When I run the project, getting the problem like the gif file.
Its my tableview function
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "requestsCell", for: indexPath) as? RequestsTableViewCell
cell?.backgroundColor = .clear
let passengerName = cell?.viewWithTag(1) as! UILabel
let bookingCreator = cell?.viewWithTag(2) as! UILabel
let department = cell?.viewWithTag(3) as! UILabel
let product = cell?.viewWithTag(4) as! UILabel
passengerName.text = "Muhammed yasheed"
bookingCreator.text = "Muhammed Yasheed"
department.text = "Programming"
product.text = "Flight"
cell?.approvedView.isHidden = false
cell?.cancelView.isHidden = true
return cell!
}
Finally I found the answer. Just add cell?.layoutIfNeeded() in cellForRowAt indexpath
cell?.layoutIfNeeded()
Related
I have a controller EditProfileController: UITableViewController and a cell EditProfileCell: UITableViewCell
extension EditProfileController {
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath) as! EditProfileCell
cell.delegate = self
return cell
}
}
My EditProfileCell
This is my TextField:
class EditProfileCell: UITableViewCell {
lazy var infoTextField: UITextField = {
let tf = UITextField()
tf.borderStyle = .none
tf.font = UIFont.systemFont(ofSize: 14)
tf.textAlignment = .left
tf.textColor = .white
tf.isUserInteractionEnabled = true
return tf
}()
}
The Problem: I can edit the infos for the textfields using the simulator. But when I'm running the app on my iPhone, I'm not able to even select the Textfield, not even the keyboard shows up.
I've tested adding cell.infoTextField.becomeFirstResponder() on the extension, and it works, but this makes only the last textfield become editable.
What I'm doing wrong?
You have to set contentView.isUserInteractionEnabled to false
extension EditProfileController {
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath) as! EditProfileCell
cell.delegate = self
cell.contentView.isUserInteractionEnabled = false
return cell
}
}
I just set my UITableCell linebreak to byWordWrapping and add this method:
override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
Now My UITableCell Images are massive, what is the best way to resize them or have the images aspect to fit?
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "commentsCell", for: indexPath)
cell.textLabel?.numberOfLines = 0
cell.textLabel?.lineBreakMode = .byWordWrapping
cell.detailTextLabel?.numberOfLines = 0
cell.detailTextLabel?.lineBreakMode = .byWordWrapping
if((self.array[indexPath.row]["profileImg"] as! String) == "")
{
cell.textLabel?.text = (self.array[indexPath.row]["username"] as! String)
cell.detailTextLabel?.text = (self.array[indexPath.row]["comment"] as! String)
cell.imageView?.image = UIImage(named: "default.png")
}
else
{
let imgUrl = URL(string:"http://auxpod.com/uploads/" + (self.array[indexPath.row]["profileImg"] as! String))
Alamofire.request(imgUrl!).responseImage { response in
DispatchQueue.main.async {
if let image = response.result.value {
cell.textLabel?.text = (self.array[indexPath.row]["username"] as! String)
cell.detailTextLabel?.text = (self.array[indexPath.row]["comment"] as! String)
cell.imageView?.image = image
}
}
}
}
return cell
}
1-
Don't load the image inside cellForRowAt as it keeps download the image every scroll so use SDWebImage that caches it after first download
2-
Create a custom cell class , hook the the height constraint of the imageView and change it according to the required aspect ratio
imageViewH.constant = imageRealHeight * imageCellWidth / imageRealWidth
I've created custom cell view in Xcode:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! FeedCell
let iniStr = self.tableArray[indexPath.row]
let fullNameArr = iniStr.components(separatedBy: "||1||")
let daFirst = fullNameArr[0]
let daSecond = fullNameArr[1]
let finalco = daSecond.components(separatedBy: "||2||")
let fString = finalco[0]
cell.questionLabel?.text = daFirst
cell.answerLabel?.text = daSecond
return cell
}
class FeedCell: UITableViewCell {
#IBOutlet weak var questionLabel: UILabel!
#IBOutlet weak var answerLabel: UILabel!
}
then hooked up everything in storyboard, set constraints and registered class: self.tableView.register(FeedCell.self, forCellReuseIdentifier: "Cell")
When I use Subtitle as class of my custom table cell everything works fine, but when I use custom FeedCell in my table cell, label's are not showing, but I am able to select cells.
Try this code to debug connections
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! FeedCell
let iniStr = self.tableArray[indexPath.row]
print(iniStr)
let fullNameArr = iniStr.components(separatedBy: "||1||")
let daFirst = fullNameArr[0]
let daSecond = fullNameArr[1]
print(daFirst)
print(daSecond)
let finalco = daSecond.components(separatedBy: "||2||")
let fString = finalco[0]
cell.questionLabel?.text = daFirst
cell.answerLabel?.text = daSecond
cell.questionLabel.backgroundColor = .red
cell.answerLabel.backgroundColor = .green
return cell
}
How to control before TableViewCell label display, my CollectionViewCell label is displayed?
My CollectionViewCell is in TableViewCell.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CollectionViewCell
let list = sections[(indexPath as NSIndexPath).row]
DispatchQueue.main.async {
cell.categoryTitle.text = list.package_name
cell.mainAssociatedURL.text = list.package_url
cell.collectionView.tag = indexPath.row
cell.collectionView.reloadData()
}
return cell
}
}
My CollectionViewCell is here,
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let list = sections[indexPath.row].packageTable[indexPath.row]
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "videoCell", for: indexPath) as! CollecitonViewCell
AsyncImageLoader.sharedLoader.imageForUrl(urlString: list.poster_url) { (image, url) -> () in
DispatchQueue.main.async(){
cell.movieTitle.text = list.name
if(url == StringResource().posterURL){
cell.imageView.image = UIImage(named: "mmcast_cover")
}else{
cell.imageView.image = image
}
}
}
return cell
}
I would like to show TableViewCell label display first and then go and display CollectionViewCell label and image.
You need to assign delegate of collectionView inside tableviewcell.
In ViewDidLoad
cell.collectionView.delegate = nil
cell.collectionView.datasource = nil
Change on delegate
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CollectionViewCell
let list = sections[(indexPath as NSIndexPath).row]
DispatchQueue.main.async {
cell.categoryTitle.text = list.package_name
cell.mainAssociatedURL.text = list.package_url
cell.collectionView.tag = indexPath.row
cell.collectionView.delegate = self
cell.collectionView.datasource = self
cell.collectionView.reloadData()
}
return cell
When I selected a row, text color turns white. however, when I scroll to refresh the row, the text color turns back to black. so I have added a logic if(cell.selected). However it didn't work. when I scroll. the code can't get into if case.Here is the code :-
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let selectedCell:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
for subview in selectedCell.subviews {
if let view = subview as? UILabel {
view.textColor = UIColor.whiteColor()
}
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var renderWidth:CGFloat = 8
var renderYPosition:CGFloat = 15
let cell:UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "idcell")
let date = UILabel(frame: CGRectMake(renderWidth, renderYPosition, 110, 20))
let dateText = "asdasdsad"
date.text = dateText
if (cell.selected) {
date.textColor = UIColor.whiteColor()
}
cell.addSubview(date)
}
You are creating a new UITableViewCell everytime. It is recommended to reuse it.
Instead of creating a new cell with this:
let cell:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "idcell")
Use this to reuse the cell:
let cell = tableView.dequeueReusableCellWithIdentifier("cellIdInStoryboard", forIndexPath: indexPath) as! UITableViewCell
The identifier should be the same as the one you set in storyboard.
The problem is with your if clause in cellForRowAtIndexPath function and also you create cell instance everytime you load the tableview.You need to reuse tableview cells.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var renderWidth:CGFloat = 8
var renderYPosition:CGFloat = 15
let cell = tableView.dequeueReusableCellWithIdentifier("idcell", forIndexPath: indexPath) as! YourTableViewCell
let date = UILabel(frame: CGRectMake(renderWidth, renderYPosition, 110, 20))
let dateText = "asdasdsad"
date.text = dateText
if (cell.selected) {
date.textColor = UIColor.whiteColor()
} else {
date.textColor = UIColor.blackColor() // You didn't add else
}
cell.addSubview(date)
}