I have added a seperator between cells to add spacing cell with separator
But when I change color of seperatorView to clearColor from blueColor it disappears,can someone help ?
Here's my code:
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 50
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell",forIndexPath: indexPath)
cell.textLabel?.text = "Hello World"
cell.textLabel?.textColor = UIColor.blackColor()
cell.backgroundColor = UIColor.clearColor()
let additionalSeperatorThickness = CGFloat(6)
let additionalSeperator = UIView(frame: CGRectMake(0,
cell.frame.size.height - additionalSeperatorThickness,
cell.frame.size.width,
additionalSeperatorThickness))
additionalSeperator.backgroundColor = UIColor.clearColor()
cell.addSubview(additionalSeperator)
let imageView = UIImageView(image: UIImage(named: "text_box_1_converted.png")!)
cell.backgroundView = imageView
cell.bringSubviewToFront(additionalSeperator)
return cell
}
Related
I am using tableView.beginUpdates() and tableView.endUpdates() to expand/contract a cell that has shadow. When the table updates, it removes all shadows from cells and then puts them back as shown here
I have tried using willDisplayCell and also tried changing the shadow to its own view, shadow on contentView and shadow on cell, none worked.
How do i keep the shadow?
extension UIView {
func addTutShadow(shadowOpacity: Float? = nil) {
self.layer.shadowColor = UIColor.black.cgColor
self.layer.shadowOffset = CGSize(width: 0, height: 3)
self.layer.shadowRadius = 12 * kHeightFactor
self.layer.shadowOpacity = shadowOpacity ?? 0.12
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell =
tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TopicListCard
cell.topic = rankedTopics[indexPath.section]
cell.backgroundColor = .clear
cell.backgroundView = UIView()
cell.selectedBackgroundView = UIView()
cell.delegate = self
cell.addTutShadow()
cell.setup()
return cell
}
var selectedIndex = -1
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if selectedIndex != indexPath.section {
selectedIndex = indexPath.section
tableView.beginUpdates()
tableView.endUpdates()
} else {
selectedIndex = -1
tableview.deselectRow(at: indexPath, animated: true)
tableView.beginUpdates()
tableView.endUpdates()
}
}
For one of my UITableView, to allow more text or large font size text to fit in the fields, I need to add or adjust font size.
I used a boolean for adjustsFontSizeToFitWidth. And clipsToBounds=true. But this does not always work for me, not sure why it's inconsistent, especially when you have bigger font size text to be included. Any correction?
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
let headerLabel = UILabel(frame: CGRect(x: 40, y: 0, width:
tableView.bounds.size.width, height: tableView.bounds.size.height))
headerLabel.textColor = UIColor.white
headerLabel.text = self.tableView(self.tableView, titleForHeaderInSection: section)
headerLabel.sizeToFit()
headerLabel.adjustsFontSizeToFitWidth = true
headerLabel.clipsToBounds=true
headerLabel.numberOfLines=0
headerLabel.lineBreakMode = NSLineBreakMode.byTruncatingTail
headerLabel.minimumScaleFactor = 0.2
headerView.addSubview(headerLabel)
return headerView
}
Delete the following may be?
headerLabel.sizeToFit()
This will allow to make
adjustsFontSizeToFitWidth
property working.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
if searching {
cell?.textLabel?.text = searchedLabour[indexPath.row]
cell?.textLabel?.sizeToFit()
cell?.textLabel?.adjustsFontSizeToFitWidth = true
cell?.textLabel?.numberOfLines = 2
cell?.textLabel?.lineBreakMode = NSLineBreakMode.byTruncatingTail
} else {
cell?.textLabel?.text = labourWordsArray[indexPath.row]
cell?.textLabel?.sizeToFit()
cell?.textLabel?.adjustsFontSizeToFitWidth = true
cell?.textLabel?.numberOfLines = 2
cell?.textLabel?.lineBreakMode = NSLineBreakMode.byTruncatingTail
}
return cell!
}
I know that adjusting UILabel height and UITableViewCell is probably a pretty standard issue, but I'm finding lots of answers that are based on the storyboard and inspectors, but not by just using Swift 3. I've created a table which covers the screen. The height for the cell is being determined as follows:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
My tableViewCell has a couple of objects in it, a UIImageView (myImage) and a UILabel (myText), set-up in a custom class. The positioning and sizing takes place in cellForRowAt.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomCell
cell.myImage.image = UIImage(named: "MyImage")
cell.myImage.layer.frame = CGRect(origin: CGPoint(x: 0, y: 10), size: (cell.myImage?.image?.size)!)
cell.myText.text = myArray[indexPath.row]
cell.myText.frame = CGRect(x: UIScreen.main.bounds.size.width * 0.25, y: 0, width: UIScreen.main.bounds.size.width * 0.7, height: 90)
cell.myText.numberOfLines = 0
return cell
}
The result is a bunch of stacked cells, overlapping each other. What should I do to adjust the height of the UILabel frame to the amount of text coming from myArray and adjust the cell height so that it's at least the height of myImage or myText?
You can make multiline label inside table view in Swift 3.
import UIKit
private let useAutosizingCells = true
class TableViewController: UITableViewController {
fileprivate let cellIdentifier = "Cell"
// MARK: - Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
//setup initial row height here
if useAutosizingCells && tableView.responds(to: #selector(getter: UIView.layoutMargins)) {
tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension
}
}
// MARK: - UITableViewDataSource
extension TableViewController {
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return detailsModel.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath)
let details = detailsModel[indexPath.row]
cell.textLabel?.text = details.title
cell.detailTextLabel?.text = details.description
if useAutosizingCells && tableView.responds(to: #selector(getter: UIView.layoutMargins)) {
cell.textLabel?.numberOfLines = 0
cell.detailTextLabel?.numberOfLines = 0
}
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)
}
I am using swift 2.0 and trying to create table view without custom cell prototype. I wrote this:
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.backgroundColor = UIColor(patternImage: UIImage(named: "ViewBg.png")!)
self.tableView.estimatedRowHeight = 100.0
self.tableView.rowHeight = UITableViewAutomaticDimension
Alamofire.request(.GET, "https://****.herokuapp.com/user/000", parameters: nil)
.responseJSON { response in
let fortuneHistoryJson = JSON(response.result.value!)["fortuneHistory"]
for var index = 0; index < fortuneHistoryJson.count; ++index {
let FortuneHistoryItem = FortuneHistory()
FortuneHistoryItem.content = fortuneHistoryJson[index]["fortune"]["content"].string
FortuneHistoryItem.date = fortuneHistoryJson[index]["createdAt"].string
FortuneHistoryItem.imageUrl = fortuneHistoryJson[index]["cupPicture"].string
self.fortuneHistoryData.append(FortuneHistoryItem)
}
self.tableView.reloadData();
}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return fortuneHistoryData.count
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell")
cell.textLabel!.text = self.fortuneHistoryData[indexPath.row].date;
cell.detailTextLabel!.text = self.fortuneHistoryData[indexPath.row].content
cell.textLabel!.numberOfLines = 0
cell.detailTextLabel!.numberOfLines = 0
cell.imageView!.kf_setImageWithURL(NSURL(string: self.fortuneHistoryData[indexPath.row].imageUrl )!, placeholderImage: UIImage(named: "ViewBg.png")!)
var itemSize: CGSize = CGSizeMake(100, 100)
UIGraphicsBeginImageContextWithOptions(itemSize, false, UIScreen.mainScreen().scale)
var imageRect: CGRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height)
cell.imageView!.image!.drawInRect(imageRect)
cell.imageView!.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return cell
}
I am expecting to enlarge cells by detailTextLabel size but they remains same and they are overlaps: http://i61.tinypic.com/1shb3b.jpg
I can't found any solution except creating custom cell prototype as #jrc mentioned. So solution is creating custom cell prototypes and using auto layout.