Strikethrough UITableView cell text after cell containing String - ios

I'd like to strikethrough all cell text in UITableView after a specific line of text.
Example (after "+")
I think I'm close, but the strikethrough isn't applied in the Simulator. (build is successful)
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "Cell")
if indexPath.row > dailyTasks.index(of: "+")! {
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: dailyTasks[indexPath.row])
attributeString.addAttribute(NSAttributedStringKey.strikethroughStyle, value: 1, range: NSMakeRange(0, attributeString.length))
cell.textLabel?.attributedText = attributeString
} else {
cell.textLabel?.text = dailyTasks[indexPath.row]
}
cell.textLabel?.font = UIFont(name: "GillSans-Light", size: 17)
cell.indentationLevel = 1
cell.textLabel?.addCharactersSpacing(1.2)
cell.backgroundColor = UIColor(red:0.99, green:0.97, blue:0.91, alpha:1.0)
cell.selectionStyle = UITableViewCellSelectionStyle.none
return cell
}

Don't set the font on the label if you are using an attributed string; instead set the font as an attribute. Here is a playground that shows how to do it:
import UIKit
import PlaygroundSupport
let label = UILabel()
let text = "sample text"
let view = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
let attributes: [NSAttributedStringKey: Any] =
[NSAttributedStringKey.font: UIFont(name: "GillSans-Light", size: 17)!,
NSAttributedStringKey.foregroundColor : UIColor.white,
NSAttributedStringKey.strikethroughStyle: 1]
label.attributedText = NSAttributedString(string: text, attributes: attributes)
view.addSubview(label)
label.sizeToFit()
PlaygroundPage.current.liveView = view

Related

iOS Swift TableviewCell issue

I have a strange problem with tableView cell.
when I scroll tableView and cell disappear and back again to the cell I understand that tableView add similar cell exactly on cell.
for example look at the picture . 3 exact text add on each other.
cellForRowAtIndexPath function :
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
if indexPath.row == 0 {
let reviewNumber = UILabel()
if (self.book.review_count == 0) {
reviewNumber.text = "\(self.lang.book["no_review"]!)"
}
if (self.book.review_count > 0) {
reviewNumber.text = "\(self.book.review_count!) \(self.lang.general["review"]!)"
}
reviewNumber.textAlignment = .Right
reviewNumber.font = UIFont(name: "Vazir", size: 14)
reviewNumber.numberOfLines = 0
reviewNumber.translatesAutoresizingMaskIntoConstraints = false
reviewNumber.textColor = UIColor.grayColor()
cell.contentView.addSubview(reviewNumber)
let voteIcon = UIImageView()
voteIcon.image = UIImage(named: "vote-icn")
voteIcon.translatesAutoresizingMaskIntoConstraints = false
cell.contentView.addSubview(voteIcon)
cell.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[v0(24)]-|",options: [],metrics: nil,views: ["v0" : voteIcon]))
let reviewIcon = UIImageView()
reviewIcon.image = UIImage(named: "review-icn")
reviewIcon.translatesAutoresizingMaskIntoConstraints = false
cell.contentView.addSubview(reviewIcon)
cell.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[v0(24)]-|",options: [],metrics: nil,views: ["v0" : reviewIcon]))
let voteNumber = UILabel()
voteNumber.text = " ۴.۵ از ۱۶۵۴رأی"
voteNumber.textAlignment = .Left
voteNumber.font = UIFont(name: "Vazir", size: 14)
voteNumber.numberOfLines = 0
voteNumber.translatesAutoresizingMaskIntoConstraints = false
voteNumber.textColor = UIColor.grayColor()
cell.contentView.addSubview(voteNumber)
cell.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-3-[v0]-3-|",options: [],metrics: nil,views: ["v0" : reviewNumber]))
cell.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-3-[v0]-3-|",options: [],metrics: nil,views: ["v0" : voteNumber]))
cell.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-35-[v0]-8-[v1(25)]",options: [],metrics: nil,views: ["v0" : voteNumber, "v1" : voteIcon, "v2" : reviewNumber, "v3" : reviewIcon]))
cell.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[v2]-8-[v3(25)]-35-|",options: [],metrics: nil,views: ["v0" : voteNumber, "v1" : voteIcon, "v2" : reviewNumber, "v3" : reviewIcon]))
}
if indexPath.row == 1 {
let userBookStatusButtn = UIButton(type: .Custom)
userBookStatusButtn.setTitle("خواهم خواند", forState: .Normal)
userBookStatusButtn.alpha = 0.2
userBookStatusButtn.titleLabel?.font = UIFont(name: "Vazir", size: 14)
userBookStatusButtn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
// userBookStatusButtn.setImage(UIImage(named: "vote-icn"), forState: .Normal)
userBookStatusButtn.translatesAutoresizingMaskIntoConstraints = false
userBookStatusButtn.backgroundColor = UIColor(red:0.0/256.0 ,green:150.0/256.0, blue:136.0/256.0 ,alpha:1 )
cell.contentView.addSubview(userBookStatusButtn)
cell.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-20-[v0(48)]-20-|",options: [],metrics: nil,views: ["v0" : userBookStatusButtn]))
cell.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-35-[v0]-35-|",options: [],metrics: nil,views: ["v0" : userBookStatusButtn]))
}
if indexPath.row == 2 {
let topBorder = UIView()
topBorder.translatesAutoresizingMaskIntoConstraints = false
topBorder.backgroundColor = UIColor(white: 0.5, alpha: 0.5)
cell.contentView.addSubview(topBorder)
let bottomBorder = UIView()
bottomBorder.translatesAutoresizingMaskIntoConstraints = false
bottomBorder.backgroundColor = UIColor(white: 0.5, alpha: 0.5)
cell.contentView.addSubview(bottomBorder)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 5
paragraphStyle.baseWritingDirection = .RightToLeft
guard let descriptionStr = self.book.description else {return cell}
let attrString = NSMutableAttributedString(string: descriptionStr)
attrString.addAttribute(NSParagraphStyleAttributeName, value:paragraphStyle, range:NSMakeRange(0, attrString.length))
let description = UILabel()
description.attributedText = attrString
description.textAlignment = .Justified
description.font = UIFont(name: "Vazir", size: 14)
description.numberOfLines = 0
description.translatesAutoresizingMaskIntoConstraints = false
description.lineBreakMode = NSLineBreakMode.ByWordWrapping
cell.contentView.addSubview(description)
let title = UILabel()
title.text = "\(self.lang.book["summery"]!)"
title.alpha = 0.2
title.textAlignment = .Center
title.font = UIFont(name: "Vazir-Bold", size: 16)
title.translatesAutoresizingMaskIntoConstraints = false
cell.contentView.addSubview(title)
cell.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[v0]-|",options: [.AlignAllCenterX],metrics: nil,views: ["v0" : title]))
cell.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-20-[v0]-20-|",options: [],metrics: nil,views: ["v0" : description]))
cell.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-8-[v1]-44-[v0]",options: [],metrics: nil,views: ["v0" : description,"v1" : title ]))
cell.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[v0]-8-|",options: [],metrics: nil,views: ["v0" : description,"v1" : title ]))
}
cell.textLabel?.text = nil
return cell
}
I get data from son file with Alamofire and reloadData :
func tableRefresh()
{
dispatch_async(dispatch_get_main_queue(), {
self.bookDetailTableView.reloadData()
})
}
what's my problem?
thanks guys.
You made a classical error when using iOS cells.
(I am a teacher, so I see this kind of error frequently... don't worry..)
some Considerations:
1) NEVER add a label to a cell. Doing so, very time a cell is reused, previous label stays here, and a new label is added
2) as label are transparent, you will see all text overlapped..
3) You are leaking memory, as no one will detach/ release labels.
so the way can be:
A)Simple for simple case
use TAGS, as Apple did on old days of cells..
(code i
make some defines:
let TEXT_TAG = 2000
add label if not present, if present get it vi TAG:
let cell = tableView.dequeueReusableCell(withIdentifier: REUSE, for: indexPath )
let text = "my text..."
if let label = cell.viewWithTag(TEXT_TAG) as? UILabel{
label.text = text
}else{
let frame = CGRect(x: 100, y: 20, width: 50, height: 50)
let label = UILabel(frame: frame)
label.text = text
cell.addSubview(label)
}
B) make a custom cell...
class CustomViewCell: UITableViewCell {
var label: UILabel?
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
let frame = CGRect(x: 100, y: 20, width: 50, height: 50)
let label = UILabel(frame: frame)
self.addSubview(label)
}
in controller simply:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: REUSE,
for: indexPath ) as! CustomViewCell
cell.label = "my text"
I get your problem,you get the cell from tableView reuseable pool.The first problem,if you want change UITableViewCell View,you need to creat a son class of it.I get a function to fix it.
I use Stackoverflow first time,I don't know where can I put my code,so I write it under this sentence.
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
for let item in cell.contentView.subviews {
item.removeFromSuperView();
}
And then using your code,it should help you.

Laggy UITableViewController

I have UITableViewController with 1000 row. Each cell contains label that fill with attributed string. Cell are self sizing cell and estimation is not correct in estimatedHeightForRowAtIndexPath so I did not use it (I don't no why my estimation cause to lag, I use NSSting.boundingRectWithSize for estimation). I have UISlider as SeekBar in bottom of my view, when I seek too middle of page main thread become busy with cellForRowAtIndexPath and even I could not show waiting progress bar like HUD progress.
Here is my cellForRowAtIndexPath:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Aya Cell") as! AyaCell
let detail = API.readingDetail[indexPath.row % API.readingDetail.count]
cell.backgroundColor = backgroundColor
switch detail {
case .Persian:
let text = translations.suras[self.sura.index-1].ayas[indexPath.row / API.readingDetail.count].text
var textBackground = UIColor.clearColor()
if translations.suras[self.sura.index-1].ayas[indexPath.row / API.readingDetail.count].shouldSelected == true {
textBackground = UIColor.yellowColor()
}
let attributedText = NSMutableAttributedString(string: text, attributes: [NSForegroundColorAttributeName: persianTextColor, NSFontAttributeName: AyaPersianFont!, NSBackgroundColorAttributeName: backgroundColor])
attributedText.appendAttributedString(NSAttributedString(string: " (\((indexPath.row / API.readingDetail.count+1).toArabicDigit()))", attributes: [NSForegroundColorAttributeName: MojezeRedColor, NSFontAttributeName:UIFont(name: "XNazanin", size: AyaFont!.pointSize*0.65)!, NSParagraphStyleAttributeName: AyaParagraphStyle]))
cell.ayaTextLabel.attributedText = attributedText
case .Arabic:
let text = self.sura.ayas[indexPath.row / API.readingDetail.count].text
var textBackground = UIColor.clearColor()
if self.sura.ayas[indexPath.row / API.readingDetail.count].shouldSelected == true {
textBackground = UIColor.yellowColor()
}
let attributedText = NSMutableAttributedString(string: text, attributes: [NSForegroundColorAttributeName: arabicTextColor, NSFontAttributeName: AyaFont!, NSBackgroundColorAttributeName: textBackground])
attributedText.appendAttributedString(NSAttributedString(string: " ﴿\((indexPath.row / API.readingDetail.count+1).toArabicDigit())﴾", attributes: [NSForegroundColorAttributeName: MojezeRedColor, NSFontAttributeName:UIFont(name: "XNazanin", size: AyaFont!.pointSize*0.65)!, NSParagraphStyleAttributeName: AyaParagraphStyle]))
cell.ayaTextLabel.attributedText = attributedText
}
return cell
}
Is there any solution for this?

Attributed text with two text alignments

Does anyone know how to achieve two different text alignments in one string?
This is what I want the textView to show:
label value
My code:
let txtView = cell.viewWithTag(77) as! UITextView
let leftStyle = NSMutableParagraphStyle()
leftStyle.alignment = NSTextAlignment.Left
let rightStyle = NSMutableParagraphStyle()
rightStyle.alignment = NSTextAlignment.Right
let attText = NSMutableAttributedString(string: "label", attributes: [NSParagraphStyleAttributeName: leftStyle])
attText.appendAttributedString(NSAttributedString(string: " "))
attText.appendAttributedString(NSAttributedString(string: "value", attributes: [NSParagraphStyleAttributeName: rightStyle]))
txtView.attributedText = attText
What I get instead:
label value
Using NSMutableParagraphStyle with NSTextTab:
let paragraph = NSMutableParagraphStyle()
paragraph.tabStops = [
NSTextTab(textAlignment: .Right, location: 100, options: [:]),
]
let str = "Label\tValue\n"
+ "foo\tbar\n"
let attributed = NSAttributedString(
string: str,
attributes: [NSParagraphStyleAttributeName: paragraph]
)
let view = UITextView(frame: CGRectMake(0, 0, 120, 120))
view.textContainer.lineFragmentPadding = 10
view.attributedText = attributed
Of course, this aligns to "tabstop", but not to the edge of UITextView. When you modify the size of the view, you have to also modify the location of NSTextTab.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
let cell = Placestableview.dequeueReusableCell(withIdentifier: "cell", for: indexPath);
//first text
let attributestitle = [NSAttributedStringKey.font:
UIFont(name: "Helvetica-Bold", size: 15.0)!,
NSAttributedStringKey.foregroundColor: UIColor.black] as [NSAttributedStringKey: Any]
//second text
let attributedString = NSMutableAttributedString(string: "\t"+String(places[indexPath.row].distance!)+" miles", attributes: [NSAttributedStringKey.font: UIFont(name: "Helvetica-Bold", size: 8.0)!,NSAttributedStringKey.foregroundColor: UIColor.black])
let myParagraphStyle = NSMutableParagraphStyle()
myParagraphStyle.alignment = .right
myParagraphStyle.tabStops = [
NSTextTab(textAlignment: .right, location: 300, options: [:]),
]
let attributedStringtitle = NSMutableAttributedString(string: places[indexPath.row].title!, attributes: attributestitle)
//adding the right alignment to the second text alone
attributedString.addAttributes([.paragraphStyle: myParagraphStyle], range: NSRange(location: 0, length: attributedString.length))
//combining two texts with different alignments.
let combination = NSMutableAttributedString()
combination.append(attributedStringtitle)
combination.append(attributedString)
cell.textLabel?.attributedText = combination
return cell;
}

How to set frame of elements inside CollectionViewCell?

I have a custom cell class that looks like this:
class CollectionViewCell: UICollectionViewCell {
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
var textLabel: UILabel = UILabel()
var buttonView: UIButton!
var id: String = String()
override init(frame: CGRect) {
super.init(frame: frame)
let textFrame = CGRect(x: buttonView.center.x - self.frame.width/2, y: 90, width: frame.size.width, height: frame.size.height/3)
textLabel = UILabel(frame: textFrame)
textLabel.font = UIFont.systemFontOfSize(UIFont.smallSystemFontSize())
textLabel.textAlignment = .Center
contentView.addSubview(textLabel)
buttonView = UIButton.buttonWithType(UIButtonType.System) as! UIButton
buttonView.frame = CGRect(x: frame.width / 2 - frame.width * 0.4, y: 0 + textLabel.frame.size.height, width: frame.size.width * 0.8, height: frame.size.height * 0.8 )
contentView.addSubview(buttonView)
}
}
and the func where I dequeue cells looks like this:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
switch collectionView{
case collectionViewOne!:
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewDetailCell", forIndexPath: indexPath) as! CollectionViewDetailCell
cell.backgroundColor = UIColor.whiteColor()
cellHeightOne = cell.frame.height
cell.productLabel.text = "\(array[indexPath.row].name)"
cell.textLabel.numberOfLines = 0
let myText = NSString(format: self.array[indexPath.row].description)
let paragraphStyle = NSMutableParagraphStyle() as NSMutableParagraphStyle
paragraphStyle.alignment = NSTextAlignment.Justified
let attributes: NSDictionary = [NSFontAttributeName: UIFont.systemFontOfSize(12), NSBaselineOffsetAttributeName: 0, NSParagraphStyleAttributeName: paragraphStyle, NSForegroundColorAttributeName: UIColor.grayColor()]
let attributedText = NSAttributedString(string: myText as String, attributes: attributes as [NSObject : AnyObject])
cell.textLabel.attributedText = attributedText
cell.textLabel.sizeToFit()
cell.contentView.addSubview(cell.textLabel)
return cell
I want for my labels text height to move the button down, but nothing happens, i also tried to find out the height and to send it to cell something like:
cell.textHeigth = textHeigth, and use textHeigth in
buttonView.frame = CGRect(x: frame.width / 2 - frame.width * 0.4, y: 0 + textHeigth, width: frame.size.width , height: frame.size.height * 0.8)
but then i get the error that some variable is nil, but I tryed printing the variable and before sending it to customClass it is not nil
Implement the below delegate method to set width and height of collection view cell
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(width,height);
}
I managed to put my button under the text with this code:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewDetailCell", forIndexPath: indexPath) as! CollectionViewDetailCell
let myText = NSString(format: newString)
let paragraphStyle = NSMutableParagraphStyle() as NSMutableParagraphStyle
paragraphStyle.alignment = NSTextAlignment.Justified
let attributes: NSDictionary = [NSFontAttributeName: UIFont.systemFontOfSize(12), NSBaselineOffsetAttributeName: 0, NSParagraphStyleAttributeName: paragraphStyle, NSForegroundColorAttributeName: UIColor.grayColor()]
let attributedText = NSAttributedString(string: myText as String, attributes: attributes as [NSObject : AnyObject])
cell.textLabel.attributedText = attributedText
cell.textLabel.sizeToFit()
cell.contentView.addSubview(cell.textLabel)
cell.buttonView.frame = CGRect(x: 0, y: 200 + cell.textLabel.frame.size.height, width: 10, height: 10)
return cell
}
You have to do everything in this cellForItemAtIndexPath

Multiple UICollectionView layers on same cells

This might be a compound question, so please bear with me.
Upon finishing loading objects from Parse and populating the CollectionViewCells with them, it shows a strange bouncing animation even if I explicitly set self.collectionView?.bounces = false. Moreover, the labels are clearly drawing over each other and you can see a picture bouncing under another.
Here are the bugs I'm seeing:
https://youtu.be/q306SGM_tLE and https://youtu.be/gK9HN877nRo
This is how I handle the cells:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("itemCell", forIndexPath: indexPath) as UICollectionViewCell
let nameLabelFont = UIFont(name: "Lato-Regular", size: 13)
let priceLabelFont = UIFont(name: "Lato-Regular", size: 10)
var priceLabel = UILabel(frame: CGRectMake(0, 0, 50, 40))
var name = UILabel(frame: CGRectMake(0, 180, self.view.frame.size.width/2-5, 20))
var unitLabel = UILabel(frame: CGRectMake(0, 200, self.view.frame.size.width/2-5, 20))
var addToCartButton = UIButton(frame: CGRectMake(0, 220, 100, 30))
if self.productImageArray.count != 0 {
var image = self.productImageArray.objectAtIndex(indexPath.row) as UIImage
var imageView = UIImageView(image: image)
imageView.frame.size = CGSize(width: 187.5, height: 180)
name.attributedText = NSAttributedString(
string: self.productArray.objectAtIndex(indexPath.row).valueForKey("name") as NSString,
attributes: [NSFontAttributeName : nameLabelFont!, NSForegroundColorAttributeName : UIColor.blackColor()])
var price:Float = self.productArray.objectAtIndex(indexPath.row).valueForKey("price_tipsy") as Float
priceLabel.backgroundColor = UIColor.redColor()
priceLabel.attributedText = NSAttributedString(
string: "$\(price)",
attributes: [NSFontAttributeName : nameLabelFont!, NSForegroundColorAttributeName : UIColor.whiteColor()])
unitLabel.attributedText = NSAttributedString(
string: self.productArray.objectAtIndex(indexPath.row).valueForKey("unit") as NSString,
attributes: [NSFontAttributeName : nameLabelFont!, NSForegroundColorAttributeName : UIColor.blackColor()])
addToCartButton.setAttributedTitle(NSAttributedString(
string: "ADD TO CART",
attributes: [NSFontAttributeName : priceLabelFont!, NSForegroundColorAttributeName : UIColor.redColor()]),
forState: UIControlState.Normal)
addToCartButton.backgroundColor = UIColor.whiteColor()
addToCartButton.layer.borderColor = UIColor.redColor().CGColor
addToCartButton.layer.borderWidth = 1
addToCartButton.layer.cornerRadius = 5
cell.addSubview(imageView)
cell.addSubview(name)
cell.addSubview(priceLabel)
cell.addSubview(unitLabel)
cell.addSubview(addToCartButton)
} else {
cell.backgroundColor = UIColor.whiteColor()
}
return cell
}
EDIT
This is my UI
and its Debug View Hierarchy
after a few times of pull to refresh, Debug View Hierarchy shows multiple layers on the same cell, number of layers corresponding to number of refreshes.
EDIT 2
Here is my custom cell class:
override init(frame: CGRect) {
super.init(frame: frame)
}
func setup(frameWidth:CGFloat, image: UIImage, name: NSString, price:Float, unit:NSString) {
let nameLabelFont = UIFont(name: "Lato-Regular", size: 13)
let priceLabelFont = UIFont(name: "Lato-Regular", size: 10)
var priceLabel = UILabel(frame: CGRectMake(0, 0, 50, 40))
var nameLabel = UILabel()
var unitLabel = UILabel()
var addToCartButton = UIButton(frame: CGRectMake(0, 220, 100, 40))
nameLabel = UILabel(frame: CGRectMake(0, 180, frameWidth/2-5, 20))
unitLabel = UILabel(frame: CGRectMake(0, 200, frameWidth/2-5, 20))
nameLabel.backgroundColor = UIColor.whiteColor()
unitLabel.backgroundColor = UIColor.whiteColor()
var imageView = UIImageView(image: image)
imageView.frame.size = CGSize(width: frameWidth/2-5, height: 180)
nameLabel.attributedText = NSAttributedString(
string: name,
attributes: [NSFontAttributeName : nameLabelFont!, NSForegroundColorAttributeName : UIColor.blackColor()])
priceLabel.backgroundColor = UIColor.redColor()
priceLabel.attributedText = NSAttributedString(
string: "$\(price)",
attributes: [NSFontAttributeName : nameLabelFont!, NSForegroundColorAttributeName : UIColor.whiteColor()])
unitLabel.attributedText = NSAttributedString(
string: unit,
attributes: [NSFontAttributeName : nameLabelFont!, NSForegroundColorAttributeName : UIColor.blackColor()])
addToCartButton.setAttributedTitle(NSAttributedString(
string: "ADD TO CART",
attributes: [NSFontAttributeName : priceLabelFont!, NSForegroundColorAttributeName : UIColor.redColor()]),
forState: UIControlState.Normal)
addToCartButton.backgroundColor = UIColor.whiteColor()
addToCartButton.layer.borderColor = UIColor.redColor().CGColor
addToCartButton.layer.borderWidth = 1
addToCartButton.layer.cornerRadius = 5
self.contentView.addSubview(imageView)
self.contentView.addSubview(nameLabel)
self.contentView.addSubview(priceLabel)
self.contentView.addSubview(unitLabel)
self.contentView.addSubview(addToCartButton)
}
And in terms of UICollectView class, self.collectionView?.reloadData() is called when RefreshController is pulled, and here:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("itemCell", forIndexPath: indexPath) as ShopCell
cell.setup(self.view.frame.width, image: UIImage(), name: "AAA", price: 20.00, unit: "aaa")
cell.contentView.backgroundColor = UIColor.orangeColor()
return cell
}

Resources