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)
}
Related
I have a UIViewController in mainstoryboard, and it contains a tableview, it just shows the labels, not exciting things. And when I click one of the cells it push me to the detailVC. The problem is starting here, when I come back from detailVC, the cell that i pushed, is still looking selected. And it looks gross. I tried everything that i can. And lastly cells are custom cell.
P.s.: I have to use swift 2.3 in this project.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableVieww.dequeueReusableCellWithIdentifier("cellNew", forIndexPath: indexPath) as! AltKategoriNewCell
let data = self.katData[indexPath.row]
cell.textLabelNew?.text = data["CatogryName"] as? String
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableVieww.dequeueReusableCellWithIdentifier("cellNew", forIndexPath: indexPath) as! AltKategoriNewCell
let data = self.katData[indexPath.row]
cell.textLabelNew?.text = data["CatogryName"] as? String
cell.contentView.backgroundColor = UIColor.lightGrayColor()
cell.backgroundColor = UIColor.lightGrayColor()
cell.textLabelNew?.textColor = UIColor.blackColor()
urunlerList.altKatDic = self.katData[indexPath.row]
performSegueWithIdentifier("urunlerList", sender: nil)
}
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableVieww.dequeueReusableCellWithIdentifier("cellNew", forIndexPath: indexPath) as! AltKategoriNewCell
cell.contentView.backgroundColor = UIColor.whiteColor()
cell.backgroundColor = UIColor.whiteColor()
cell.textLabelNew?.textColor = UIColor.blackColor()
}
TableView
Attributes
First thing that is wrong is that you are dequeueing the cell in didSelectRowAtIndexPath and didDeselectRowAtIndexPath methods. UITableView does not expect you to do that there. If you need to get the cell in didSelectRowAtIndexPath, you can ask
let cell = tableView.cellForRow(at: indexPath)
UITableViewCell has selectedBackgroundView and, UILabel has highlightedTextColor. Knowing that, you can just set up appropriately your cell, and then you won't need to modify its properties on selection/deselection, like this:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cellNew", forIndexPath: indexPath) as! AltKategoriNewCell
if nil == cell.selectedBackgroundView {
cell.selectedBackgroundView = UIView()
cell.selectedBackgroundView?.backgroundColor = UIColor.lightGrayColor()
}
let data = self.katData[indexPath.row]
cell.textLabelNew?.text = data["CatogryName"] as? String
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
urunlerList.altKatDic = self.katData[indexPath.row]
performSegueWithIdentifier("urunlerList", sender: nil)
}
Having this, your implementation of didSelectRowAtIndexPath and didDeselectRowAtIndexPath can be removed.
I have one custom style UITableviewCell and it have default height for other I am using tableview cell with style of subtitle and I want to make subtitle to be multiple lines. And it's doing good but having problem with calculating tableview cell height and I use UITableviewAutomaticDimension but its not working.
Here is my code
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if(indexPath.row==0){
var cell: ImageCell! = tableView.dequeueReusableCellWithIdentifier("ImageCell") as? ImageCell
if cell == nil {
tableView.registerNib(UINib(nibName: "ImageCell" ,bundle: nil), forCellReuseIdentifier: "ImageCell")
cell = tableView.dequeueReusableCellWithIdentifier("ImageCell") as? ImageCell
}
return cell
}else{
var cell: UITableViewCell? = tableView.dequeueReusableCellWithIdentifier("SubtitleCell")
if (cell == nil) {
cell = UITableViewCell.init(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "SubtitleCell")
cell!.backgroundColor = UIColor.brownColor()
cell!.textLabel?.font = UIFont.init(name: "HelveticaNeue", size: 15)
cell!.textLabel?.textColor = UIColor.blackColor()
cell!.textLabel?.highlightedTextColor = UIColor.lightGrayColor()
cell!.selectedBackgroundView = UIView.init()
}
cell!.textLabel?.text = "TExt"
cell!.detailTextLabel!.text="Keep in mind that UITableView is defined as an optional in the function, which means your initial cell declaration needs to check for the optional in the property. Also, the returned queued cell is also optional, so ensure you make an optional cast to UITableViewCell. Afterwards, we can force unwrap because we know we have a cell."
allowMultipleLines(cell!)
cell!.imageView?.image = UIImage(named: "IconProfile")
return cell!
}
}
func allowMultipleLines(tableViewCell:UITableViewCell) {
tableViewCell.detailTextLabel?.numberOfLines = 0
tableViewCell.detailTextLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if(indexPath.row==0){
return 172
}else{
return UITableViewAutomaticDimension
}
}
in your viewDidLoad() Method you need to write
yourTableViewName.rowHeight = UITableViewAutomaticDimension
And Add One more method
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if(indexPath.row==0){
return 172
}else{
return UITableViewAutomaticDimension;
}
}
i hope this will help you
I have an MGSwipeTableCell added in my tableView like this:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let reuseIdentifier = "Cell"
var cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier) as! MGSwipeTableCell!
if cell == nil {
cell = MGSwipeTableCell(style: UITableViewCellStyle.Default, reuseIdentifier: reuseIdentifier)
}
cell.delegate = self
cell.backgroundColor = blue
}
All works fine. But now If I tap the cell the color of the cell changes for maybe a second to white and then to green (default color is blue). This is what should happen if the cell has been pressed:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let mySelectedCell = tableView.cellForRowAtIndexPath(indexPath) as! MGSwipeTableCell!
mySelectedCell.backgroundColor = green
mySelectedCell.detailTextLabel?.text = "Test"
}
So the color should only change to green not to white and then to green and a detailTextLabel with the text "Test" should be displayed.
I hope you can help me to solve this problem. I don't really know what to do.
Solution:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let reuseIdentifier = "Cell"
var cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier) as! MGSwipeTableCell!
cell = MGSwipeTableCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: reuseIdentifier)
cell.selectionStyle = UITableViewCellSelectionStyle.None
cell.delegate = self
cell.backgroundColor = blue
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let reuseIdentifier = "Cell"
var cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier) as! MGSwipeTableCell!
if cell == nil {
//Change this line
cell = MGSwipeTableCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: reuseIdentifier)
}
//And this line
cell.selectionStyle = UITableViewCellSelectionStyle.None
cell.delegate = self
cell.backgroundColor = blue
}
leave everything as is inside "didSelectRowAtIndexPath"
I posted earlier - on how to stop multiple cells from being able to be selected in my table view - the answer was to use tableView.AllowsMultipleSelections=false
I have been trying and failing miserably since, essentially it doesn't seem to be obeying that rule.
My desired output should be one cell is selected and has a green tick next to it (this works) if a new cell is selected, the old tick is removed and moved to the new current cell.
I have tried many approaches and the latest is to create my own index path from the last index selected - then set the accessory view to nil and change selected to false. This also does not work. Please could someone help me with what I might be doing wrong.
Thanks
import UIKit
class QuizViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{
var countries = ["Germany", "France", "England", "Poland", "Spain"];
var selected = -1;
#IBOutlet var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.allowsMultipleSelection = false;
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1;
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.countries.count;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
println("indexpath \(indexPath.row)");
let cell = self.tableView.dequeueReusableCellWithIdentifier("CountryCell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel?.text = self.countries[indexPath.row];
cell.textLabel?.textAlignment = NSTextAlignment.Center;
let countryImage = String(self.countries[indexPath.row]) + ".png";
cell.imageView?.image = UIImage(named: countryImage);
let image: UIImageView = UIImageView();
cell.accessoryView = image;
return cell;
}
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
var cell = self.tableView.dequeueReusableCellWithIdentifier("CountryCell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel?.text = self.countries[indexPath.row];
cell.textLabel?.textAlignment = NSTextAlignment.Center;
let countryImage = String(self.countries[indexPath.row]) + ".png";
cell.imageView?.image = UIImage(named: countryImage);
let imageName = "tick.png";
let image: UIImageView = UIImageView(image: UIImage(named: imageName));
cell.accessoryView = image;
if(selected != -1){
//lets remove the tick from the previously selected cell and set selected to false
var cell = self.tableView.dequeueReusableCellWithIdentifier("CountryCell", forIndexPath: NSIndexPath(forRow: selected, inSection: 0)) as UITableViewCell
cell.accessoryView = nil;
cell.selected = false;
}
selected = indexPath.row;
}
}
Attempt at proposed solution
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("CountryCell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel?.text = self.countries[indexPath.row];
cell.textLabel?.textAlignment = NSTextAlignment.Center;
let countryImage = String(self.countries[indexPath.row]) + ".png";
cell.imageView?.image = UIImage(named: countryImage);
if(cell.selected){
println("cell was set to selected at \(indexPath.row)");
let imageName = "tick.png";
let image: UIImageView = UIImageView(image: UIImage(named: imageName));
cell.accessoryView = image;
}else{
println("cell was NOT set to selected at \(indexPath.row)");
let image: UIImageView = UIImageView();
cell.accessoryView = image;
}
return cell;
}
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
var cell = self.tableView.dequeueReusableCellWithIdentifier("CountryCell", forIndexPath: indexPath) as UITableViewCell
cell.selected = true;
if(selected != -1){
var previous = self.tableView.dequeueReusableCellWithIdentifier("CountryCell", forIndexPath: NSIndexPath(forRow: selected, inSection: 0)) as UITableViewCell
previous.selected = false;
}
selected = indexPath.row;
tableView.reloadData();
}
Don't modify the cell in didSelectRowAtIndexPath. Set the country as "selected" and the previous one as "not selected" and then reload the table data. Use the selected (or not) state to update the cell visually when cellForRowAtIndexPath is called.
I've been trying to get a UISwitch shown as an accessory view in my UITableViewCell, but it just won't show up. I think there might be something I'm overseeing.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = self.tableView.dequeueReusableCellWithIdentifier("enabledCell") as? UITableViewCell
if cell == nil {
cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "enabledCell")
}
cell!.textLabel?.text = "Alarm enabled"
var enabledSwitch = UISwitch(frame: CGRectZero) as UISwitch
enabledSwitch.on = true
cell!.accessoryView = enabledSwitch
return cell!
}