I would like to select the first cell in the tableview and give it an alpha of 100%, only for the first cell and this style should not be reused by the dequeueReusableCell method.
if indexPath.row == 0 {
cell.imageView.alpha = 1.0
}
When I use above code within the function cellForItemAt it will be set alpha of 1.0 to every 7th cell. I only want it to be applied on the first cell and the style should not be reused... How can I solve this? Should I create another custom cell or is there a kind approach in code do achieve this?
You should reset the imageView's alpha if it's not the first cell.
if indexPath.row == 0 {
cell.imageView.alpha = 1.0
}
else {
cell.imageView.alpha = 0.0 // or whatever alpha value you need
}
Create a UITableViewCell xib subclass. Then you can register the same cell with different reuseIdentifiers when you're setting up your table.
tableView.register(CustomCell, forCellReuseIdentifier: "AlphaCell")
tableView.register(CustomCell, forCellReuseIdentifier: "{REUSE_ID}")
When you setup your cell in:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
dequeue "AlphaCell" for your first cell, and "{REUSE_ID}" for your other cells.
Related
In my viewcontroller,
I added my tableview (#IBOutlet weak var MyTableView: UITableView!) in which I added several custom cells with specific size depending on the elements needed inside.
I call each cell with identifier :
func tableView(_ MyTableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = MyTableView.dequeueReusableCell(withIdentifier: "FirstCell") as! FirstCell
self.SettingsTableView.rowHeight = 220
return cell
} else if indexPath.row == 1 { etc etc...
And at a specific row, I've added a UIswitch directly in the cell that should expand / collapse this cell depending if it's ON or OFF.
I'd like to make it work a bit like the SelectRowAtIndexPath method and animate it to make appear 3 textFields below....
I'm looking for hours a solution, if you have any idea... Any suggestion is welcome !
If your switch is properly hooked up to the cell, the problem may be that you didn't refresh the UITableView, and thence it doesn't expand.
I need to shift the cell of tableview in swift to last position and change it's background colour after selection of the same cell.
use the swap function to swap() the two indexes then use table.reloadData(), and use an index variable set it to the last index and in cellForRow method change cell.backgroundColor in case indexPath.row == that index
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let clickedCell = tableView.cellForRow(at: indexPath)
//Change cell with the last cell.
}
Hope this will help. You dont need to reload the entire tableviewcontroller.
I'm trying to implement an expanding cell using this
here is my heightForRowAt indexPath
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
switch indexPath.row {
case 0 : return 70
case 1 :
let cell = self.tableView(tableView, cellForRowAt: indexPath)
if let datePickerCell = cell as? MyDatePickerCell {
return datePickerCell.datePickerHeight()
}
return 260
case 2 : return 80
default : return 60
}
}
The app gets stuck at
let cell = self.tableView(tableView, cellForRowAt: indexPath)
the UITableView is embeded in a modal UIViewController
why is this happening?
I think what is happening is that your tableView is looking for the heights of your cells before loading them in the first place. So you are trying to retrieve the cell before it is loaded, causing a crash.
To fix this you could create a dummy cell before loading the tableView and using that cell in heightForCell. That's more applicable for calculating variable heights for each cell though. In this case maybe hardcode the start height and then use the cell for heights after that.
No article explains it clearly regarding my query, I have three cells in a static table and I want to hide second cell when users taps on first cell. Any kind of help is appreciated.
Although you cannot stop the static table from trying to show your cells, you can set their height to zero, making them effectively invisible:
Add this method to your table view controller delegate class:
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
let cell = super.tableView(tableView, cellForRowAtIndexPath: indexPath)
return cell == myHiddenCell ? 0 : super.tableView(tableView, heightForRowAtIndexPath:indexPath)
}
In the didSelectCellAtIndexPath method, you can set the height to 0 to hide it :
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.row == 0 {
let indexPath = NSIndexPath(forItem: 1, inSection : 0)
let secondCell = tableview.cellForRowAtIndexPath(indexPath)
secondCell.frame.size.height = 0;
self.view.layoutSubviews()
}
}
If you want an animation, just put self.view.layoutSubviews() in an UIView animation method UIView.animateWithDuration... etc
For me, setting the height to 0 for some cells and another height for other cells wasn't an option, as all my cells have different height.
I created another cell in Storyboard, and set row height of 0 (in size inspector). Then in the code, I show the cell with height = 0 if I want to hide it, if not, I show the other cell:
if (hideCell) {
let hiddenCell = tableView.dequeueReusableCell(withIdentifier: "hiddenCell",for: indexPath) as! TheWallTableViewCell
return hiddenCell
}
else {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell",for: indexPath) as! TheWallTableViewCell
return cell
}
I have a UITableView with a transparent background color and cells which also have a transparent background color. When I reload my tableView with:
dataSource = some new data
tableView.reloadData()
I can see the new cells overlap the old ones.
I did try to use use
tableView.beginUpdates()
// remove all rows here
change data source
// insert new rows here
tableView.endUpdates()
but it did not work. I tried as well tableView.reloadRowsAtIndexPath(...) but still no luck.
And finally I set all my cells and my table view to clear graphic context when redrawn but it did not manage to fix this issue.
Any help would be greatly appreciated.
My cell creation function:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("suggestioncell")
cell.backgroundColor = UIColor.whiteColor().alpha(0.1)
cell.textLabel?.text = (suggestions![indexPath.row] as! SVPlacemark).formattedAddress
cell.clearsContextBeforeDrawing = true
cell.contentView.clearsContextBeforeDrawing = true
return cell
}
Try overriding prepareForReuse in you UITableViewCell subclass, and reset content there.
Here's what the documentation says about that:
Prepares a reusable cell for reuse by the table view's delegate.
If a UITableViewCell object is reusable—that is, it has a reuse identifier—this method is invoked just before the object is returned from the UITableView method dequeueReusableCellWithIdentifier:. For performance reasons, you should only reset attributes of the cell that are not related to content, for example, alpha, editing, and selection state. The table view's delegate in tableView:cellForRowAtIndexPath: should always reset all content when reusing a cell. If the cell object does not have an associated reuse identifier, this method is not called. If you override this method, you must be sure to invoke the superclass implementation.
Custom UITableViewCell class:
class customCell: UITableViewCell {
override func prepareForReuse() {
self.textLabel?.text = nil
}
}
In your cellForRowAtIndexPath method:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("suggestionCell", forIndexPath: indexPath) as! customCell
cell.backgroundColor = UIColor.whiteColor().alpha(0.1)
cell.textLabel?.text = (suggestions![indexPath.row] as! SVPlacemark).formattedAddress
return cell
}
And, of course in your XIB/Storyboard, set the cell class to CustomCell, and set its reuse identifier.