UITableView set background color - ios

I change the background color of the UITableViewCells in the tableView:cellForRowAtIndexPath method
if(indexPath.row % 2 == 0){
cell.backgroundColor = ...
} else{
cell.backgroundColor = ...
}
But that only change the color of the cell amount specified in tableView:numberOfRowsInSection (As seen in the attached picture, there are white cells after the first four)
Is it possible to change the color of all cells that are displayed on screen?

Open Storyboard
Select your UITableView
Open Attribute inspector
Scroll to View group
Select background color for entire table.

If you want the cell background color to continue to alternate, then you need to lie about how many rows are in the table. Specifically, in tableView:numberOfRowsInSection you need to always return a number that will fill the screen, and in tableView:cellForRowAtIndexPath, return a blank cell for rows that are beyond the end of the table. The following code demonstrates how to do this, assuming that self.dataArray is an NSArray of NSStrings.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ( self.dataArray.count < 10 )
return( 10 );
else
return( self.dataArray.count );
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"SimpleCell"];
if ( indexPath.row % 2 == 0 )
cell.backgroundColor = [UIColor orangeColor];
else
cell.backgroundColor = [UIColor redColor];
if ( indexPath.row < self.dataArray.count )
cell.textLabel.text = self.dataArray[indexPath.row];
else
cell.textLabel.text = nil;
return cell;
}

Try like this:-
self.tableView.backgroundView.backgroundColor = [UIColor blueColor];

In swift, you can change tableview background color or you can set image to tableview background color like this;
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.view.backgroundColor = UIColor(patternImage: UIImage(named: "background.png")!)
self.tableView.backgroundColor = UIColor.clearColor()
}
// change cell text color and background color
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.backgroundColor = UIColor.clearColor()
}

I used this to color alternate cell in a table view
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,
forRowAtIndexPath indexPath: NSIndexPath) {
if (indexPath.row % 2 == 0)
{
cell.backgroundColor = UIColor.grayColor()
}
else
{
cell.backgroundColor = UIColor.whiteColor()
}
}

You need to set the tableview's backgroundView to nil and its backgroundColor to the desired color.

For SWIFT
Thanks to #Irshad Qureshi I was able to get alternating background colors for my prototype cells by adding the following in my cellForRowAtIndexPath:
if (indexPath.row % 2 == 0)
{
cell!.backgroundColor = UIColor.groupTableViewBackgroundColor()
}
else
{
cell!.backgroundColor = UIColor.whiteColor()
}

Swift 5 and up
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
//Set Clear Backcolor
//cell.backgroundColor = .clear
//or in your case
if ( indexPath.row % 2 == 0 )
cell.backgroundColor = .orange
else
cell.backgroundColor = .red
}

Related

Reuse error tableView with cell design programmatically

I have a problem with my tableView who I managed specially, i need to delete and add row really often. My cell are designed programmatically. I update my array who depend my cells and called self.tableView.reloadData() but this don't remove the cells I need and update the tableView like my array.
Cause to the reuse and my design of cell (programmatically) I need to check if the cell is always design or not. And the problem come from here.
When I called tableView.reloadData() my data are not properly reload, so I need to delete All view in the cells: indicate that the cells are not design, to design the new cell ... Of course I can just update the visible cells (with tableView.visibleCells), so this work but how can I update my other not-visible cells ?
Maybe I have an architecture problem? If so, what is the best way to delete and insert a row in the TableView with a indexPath defined? Or, how programmatically design the cell only one time?
Code:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return user.lobbySurvey.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier:"Celll") as! CardCell
for survey in user.lobbySurvey{
let index = user.lobbySurvey.index(where: {
//get the current index is nedeed else the cells reuse lazy
$0 === survey
})
if indexPath.row == index{
var surveyState : UserSurvey.state
surveyState = survey.state
switch surveyState{
case .selectSurvey:
cell.drawCard(statutOfCard: .selectSurvey)
case .goSurvey:
cell.drawCard(statutOfCard: .goSurvey(picture: survey.picture))
case .surveyEnded:
print("survey Ended")
case .surveyWork:
print("survey in progress to vote")
case .surveyWaiting:
cell.drawCard(statutOfCard: .surveyWaiting(selfSurveyId: survey.id, timeLeft: survey.timeLeft, picture: survey.picture))
case .buyStack:
cell.drawCard(statutOfCard: .buyStack(supView : self.view))
}
}
}
cell.delegate = self
cell.delegateCard = self
cell.layer.backgroundColor = UIColor.clear.cgColor
cell.backgroundColor = .clear
tableView.backgroundColor = .clear
tableView.layer.backgroundColor = UIColor.clear.cgColor
return cell
}
You can have an array which is the model of your table:
NSMutableArray *model; (model can have identifier).
You can change this model whenever you want.
With this you can make your table dynamic just calling tableView.reloadData() and make whatever in cellForRow & heightForRow
- (CGFloat) tableView: (UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat height;
YourModel *model = self.model[indexPath.row];
if ([model isKindOfClass:[SomeClassTableViewCellModel class]]) {
height = 50;
} else if([model.identifier isEqualToString:#"Whatever"]){
height = 0.0f;
else{
height = kHeightForNormalCells;
}
return height;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
YourModel *model = self.model[indexPath.row];
cell = [tableView dequeueReusableCellWithIdentifier:model.identifier forIndexPath:indexPath];
if ([cell isKindOfClass:[SomeClass class]]) {
NSLog(#"Configure Cell");
[cell setModel:model];
}
return cell;
}
Moreover, your cell should have a method setModel:
- (void)setModel:(SomeTableViewCellModel *)model {
_model = model;
self.label1.text = [NSString stringWithFormat:#"%#",model.value1];
self.label2.text = [NSString stringWithFormat:#"%#",model.value2];
}
Hope this helps you.

How to detect last visible cell in UITableView

I am new in iOS development and currently working on UITableView. I want to find last visible cells on the screen of device and cells that are at the bottom of the screen must be of blue color, which should fade to green as the cell is scrolled to the top of the screen.
I have gone through these links
Link1
Link2
But could not get success. Can anyone please provide idea how to detect last cells & cell fade animation?
Get last visible cell:
if let lastCell = tableView.visibleCells.last {
// do something with lastCell
}
In Swift 3.0, you can used this tableview method.
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)
{
let intTotalrow = tableView.numberOfRows(inSection:indexPath.section)//first get total rows in that section by current indexPath.
//get last last row of tablview
if indexPath.row == intTotalrow - 1{
// call for last display
}
}
#shallowThought solution will only work if cells are already presented.
But, if you want to know the last cell when cells aren't presented yet and are going to be presented, we can create an extension for UITableView as follow:
func isLastVisibleCell(at indexPath: IndexPath) -> Bool {
guard let lastIndexPath = indexPathsForVisibleRows?.last else {
return false
}
return lastIndexPath == indexPath
}
This way, you can check tableView.isLastVisibleCell(...) multiple times until you have reached actual visible cell.
Try this code, It will work
Initially Declare
int firstIndexRow;
int lastIndexRow;
Write below code inside of ViewDidLoad()
[myTable reloadData]; //Reload because get visible last cell index row
firstIndexRow = 0;
lastIndexRow = (int)[self.myTable.indexPathsForVisibleRows lastObject].row;
NSLog(#"first : %d",firstIndexRow);
NSLog(#"Bottom : %d",lastIndexRow);
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
NSIndexPath *firstVisibleIndexPath = [[self.myTable indexPathsForVisibleRows] objectAtIndex:0];
NSIndexPath *lastObject = [self.myTable.indexPathsForVisibleRows lastObject];
firstIndexRow = (int)firstVisibleIndexPath.row;
lastIndexRow = (int)lastObject.row;
NSLog(#"first : %d",firstIndexRow);
NSLog(#"Bottom : %d",lastIndexRow);
[myTable reloadData];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [myTable dequeueReusableCellWithIdentifier:#"cell"];
if (indexPath.row == firstIndexRow) {
cell.textLabel.textColor = [UIColor blueColor];
}else if (indexPath.row == lastIndexRow) {
cell.textLabel.textColor = [UIColor greenColor];
}else{
cell.textLabel.textColor = [UIColor grayColor];
}
cell.textLabel.text =[namesArray objectAtIndex:indexPath.row];
return cell;
}

Disabling all UITableView cells except the last one - Swift

Currently working with UITableView in swift.
Since my table cell values are appending by clicking the button i want to figure out a way to disable interaction for all the previous cells, except the last one.
I've played with array-based approach a little, but it didn't work out the way i wanted.
var disabledRows = [0,1,2]
else
{
cell.backgroundColor = UIColor.orangeColor()
}
if (contains(disabledRows, indexPath.row)) {
cell.userInteractionEnabled = false
}
else {
cell.userInteractionEnabled = true
}
The issue with this approach is that i have to count all cells and then block the last one.
Maybe there are some easier approaches?
Any help much appreciated.
Update - here is my complete "cellForRowAtIndexPath" function
func tableView(tableView: UITableView, cellForRowAtIndexPath
indexPath: NSIndexPath) -> UITableViewCell
{
let cell: CustomCellForTableViewTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell") as! CustomCellForTableViewTableViewCell
if indexPath.row % 2 == 0
{
cell.backgroundColor = UIColor.purpleColor()
}
else
{
cell.backgroundColor = UIColor.orangeColor()
}
if (contains(disabledRows, indexPath.row)) {
cell.userInteractionEnabled = false
}
else {
cell.userInteractionEnabled = true
}
let quest = arrayOfQuestions[indexPath.row]
cell.setCell(type.Grocery, optionno1:type.option1, optionno2:type.option2)
cell.mainText.text = type.Grocery
cell.optionOne.backgroundColor = UIColor.redColor()
return cell
}
In cellForRowAtIndexPath: method.
if (indexPath.row == yourDataSourceArray.count - 1) {
// enable cell
} else {
// disable cell
}

disable the uitableview highlighting but allow the selection of individual cells

when you tap on a cell the row gets selected and highlighted.Now what i want to do is disable the highlighting but allow the selection.Is there a way around it.There is question that answers this but it disables both the selection and highlighting.
You can just set the cell's selection style to "None" from Storyboard:
Or from code:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
For Swift 3:
cell.selectionStyle = UITableViewCellSelectionStyle.none
For Swift 4 & above:
cell.selectionStyle = .none
Change UITableViewCell's selectedBackgroundView color to transparent.
let clearView = UIView()
clearView.backgroundColor = UIColor.clearColor() // Whatever color you like
UITableViewCell.appearance().selectedBackgroundView = clearView
or to set for a specific cell:
cell.backgroundView = clearView
Try setting cell selection style to None -
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
This will solve your problem
For Swift 3.0:
cell.selectionStyle = .none
For Swift:
UITableViewCell.selectionStyle = UITableViewCellSelectionStyle.None;
Or when you subclass a cell in swift:
class CustomCell : UITableViewCell {
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
selectionStyle = .None
}
}
in swift 3
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
To add a custom color use the below code. And to make it transparent use alpha: 0.0
cell.selectedBackgroundView = UIView(frame: CGRect.zero)
cell.selectedBackgroundView?.backgroundColor = UIColor(red:0.27, green:0.71, blue:0.73, alpha:1.0)
If you use custom color and want to give it rounded corner look use:
cell.layer.cornerRadius = 8
Also, use this for better animation and feel
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
For those looking for a programmatic way in Swift 3
cell.selectionStyle = UITableViewCellSelectionStyle.none
You can set the selection property of the cell itself in the storyboard
For Objc:
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
- (void)viewDidLoad {
[super viewDidLoad];
_tableView.allowsSelection = YES;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
.. .. .. ..
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
. . . . ..
}
For Swift 5 the best way is:
cell.selectionStyle = .none
Here is the solution for swift 3,works even in editing mode
cell.selectionStyle = .gray
cell.selectedBackgroundView = {
let colorView = UIView()
colorView.backgroundColor = UIColor.black.withAlphaComponent(0.0)
//change the alpha value or color to match with you UI/UX
return colorView
}()

Change Background of UICollectionView Cell on Tap

I have a UICollectionView that I have created programmatically. I would like for the collection view to behave in the following way:
1. User touches cell
2. Cell background color changes
3. User releases touch
4. Cell background color changes
This should be a quick color change that happens just before the selector related to the tap action is executed in which the viewcontroller containing the collection view is popped off the stack.
I have been looking at this question: UICollectionView cell change background while tap
in which there is the following summary of methods to use for this purpose:
// Methods for notification of selection/deselection and highlight/unhighlight events.
// The sequence of calls leading to selection from a user touch is:
//
// (when the touch begins)
// 1. -collectionView:shouldHighlightItemAtIndexPath:
// 2. -collectionView:didHighlightItemAtIndexPath:
//
// (when the touch lifts)
// 3. -collectionView:shouldSelectItemAtIndexPath: or - collectionView:shouldDeselectItemAtIndexPath:
// 4. -collectionView:didSelectItemAtIndexPath: or -collectionView:didDeselectItemAtIndexPath:
// 5. -collectionView:didUnhighlightItemAtIndexPath:
I am assuming I only need to implement one of the above methods from 'when touch begins' and 'when touch ends.' But no matter what I do, it appears that a background color changes and then remains changed. Here is an example of something I attempted which did not work:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//pop vc
}
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath];
cell.contentView.backgroundColor = [UIColor redColor];
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
cell.contentView.backgroundColor = [UIColor greenColor];
}
This results in the cell background color being changed only to red. I also looked at this question: UICollectionView Select and Deselect issue and tried implementing [UICollectionView selectItemAtIndexPath:animated:scrollPosition:] and calling it inside of didSelectItemAtIndexPath, but this did not work either. Collection view data source and delegate are set.
The problem is that you are changing the color on highlight and changing it back on deselect instead that on unhighlight
You should simply change this:
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
cell.contentView.backgroundColor = [UIColor greenColor];
}
to this:
- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
cell.contentView.backgroundColor = [UIColor greenColor];
}
Also, if you don't want to wait a bit before getting your highlight happen you should set the delaysContentTouches property of the collection view to NO
Edit: also ensure that you call
[collectionView deselectItemAtIndexPath:indexPath animated:NO];
inside the -didSelectItemAtIndexPath method
Swift 3 version
Add the following two methods to your view controller class:
// change background color when user touches cell
func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
cell?.backgroundColor = UIColor.red
}
// change background color back when user releases touch
func collectionView(_ collectionView: UICollectionView, didUnhighlightItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
cell?.backgroundColor = UIColor.green
}
See here for help in setting up a basic collection view in Swift.
Edit: Answer in Swift 3
var selectedIndex = Int ()
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
cell.backgroundColor = selectedIndex == indexPath.row ? UIColor.green : UIColor.red
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
selectedIndex = indexPath.row
self.yourCollctionView.reloadData()
}
Here is my solution. And I'm sure it really works.
I provide three methods to highlight a cell (selectedBackgroundView, tint cell.contentView and tint a special area).
How to use:
1. just inherit BaseCollectionViewCell and do nothing;
2. inherit and set specialHighlightedArea = UIView(), and contentView.addSubView(specialHighlightedArea), then layout it or add constraint to use Auto Layout;
3. if you don't need highlight effect, just write a method named 'shouldHighlightItemAtIndexPath' defined by UICollectionViewDelegate and make it return false, or set cell.shouldTintBackgroundWhenSelected = false and set specialHighlightedArea = nil and remove it from superView.
/// same with UITableViewCell's selected backgroundColor
private let highlightedColor = UIColor(rgb: 0xD8D8D8)
/// you can make all your collectionViewCell inherit BaseCollectionViewCell
class BaseCollectionViewCell: UICollectionViewCell {
/// change it as you wish when or after initializing
var shouldTintBackgroundWhenSelected = true
/// you can give a special view when selected
var specialHighlightedArea: UIView?
// make lightgray background display immediately(使灰背景立即出现)
override var isHighlighted: Bool {
willSet {
onSelected(newValue)
}
}
// keep lightGray background until unselected (保留灰背景)
override var isSelected: Bool {
willSet {
onSelected(newValue)
}
}
func onSelected(_ newValue: Bool) {
guard selectedBackgroundView == nil else { return }
if shouldTintBackgroundWhenSelected {
contentView.backgroundColor = newValue ? highlightedColor : UIColor.clear
}
if let area = specialHighlightedArea {
area.backgroundColor = newValue ? UIColor.black.withAlphaComponent(0.4) : UIColor.clear
}
}
}
extension UIColor {
convenience init(rgb: Int, alpha: CGFloat = 1.0) {
self.init(red: CGFloat((rgb & 0xFF0000) >> 16) / 255.0, green: CGFloat((rgb & 0xFF00) >> 8) / 255.0, blue: CGFloat(rgb & 0xFF) / 255.0, alpha: alpha)
}
}
Simple binary logic solution. Works with Swift 3 and 4:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt
indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath) as! CategoryCell
let lastCellColor = cell.backgroundColor
if cell.isSelected {cell.backgroundColor = .green} else {cell.backgroundColor = lastCellColor}
}
Add all the subviews inside contentView,
use backgroundView and selectedBackgroundView from UICollectionViewCell. Do not set contentView.backgroundColor.
// Add this inside your cell configuration.
private func setupSelectionColor() {
let backgroundView = UIView()
backgroundView.backgroundColor = .white
self.backgroundView = backgroundView
let selectedBackgroundView = UIView()
selectedBackgroundView.backgroundColor = .orange
self.selectedBackgroundView = selectedBackgroundView
}
// Add the deselection inside didSelectCallback
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
collectionView.deselectItem(at: indexPath, animated: true)
}

Resources