13

In my app, I want to display multi-column tableView in iPad.

So, I used a cocoa control from :MultiColumn TableView

it works fine for me, But I want to display the rows in alternate color.

For this, I am not able to find where I change the code for it.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
user1673099
  • 3,293
  • 7
  • 26
  • 57

13 Answers13

45
- (void)tableView: (UITableView*)tableView willDisplayCell: (UITableViewCell*)cell forRowAtIndexPath: (NSIndexPath*)indexPath
{
 
        if(indexPath.row % 2 == 0)
              cell.backgroundColor = [UIColor redColor];
        else
              cell.backgroundColor = [UIColor whiteColor];
}
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
  • 1
    Note that this solution does not work well with tables with sections. You would want the **absolute** row of the table, which can be found with this category I implemented: https://github.com/edekhayser/UITableView-RowConvenience – erdekhayser Feb 25 '15 at 23:47
  • The solution works perfect!!, But this is strange that, if you not write else part, it will not work after scrolling!!!, shouldn't table view take default color for else part. – guru Sep 29 '18 at 13:08
9

use the indexPath in the cellForRowAtIndexPath to get the desired results.

    if( [indexPath row] % 2){
          cell.backgroundColor=[UIColor whiteColor];
    } 
    else{
          cell.backgroundColor=[UIColor purpleColor];
    }

You will have this delegate method in the class which is implementing the UITableViewDelegate

nsgulliver
  • 12,655
  • 23
  • 43
  • 64
  • Thanks for reply! There are so many files in it.so i can't able to find the method for change the code. If you ever used this control, then please tell me the file name in which i will change the code. – user1673099 Mar 19 '13 at 11:07
  • You need to set this in your class which is implementing the `UITableViewDelegate` – nsgulliver Mar 19 '13 at 11:08
6
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    .......

    if (indexPath.row % 2 == 0) {
       cell.backgroundColor = [UIColor lightGrayColor];
    }
    else
    {
        cell.backgroundColor = [UIColor darkGrayColor];
    }
    .......

    return cell;
}
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Manann Sseth
  • 2,745
  • 2
  • 30
  • 50
5

First you need to take modulas of each row.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    .......

    if (indexPath.row % 2 == 0) {
       cell.backgroundColor = [UIColor lightGrayColor];
    }
    else
    {
        cell.backgroundColor = [UIColor darkGrayColor];
    }
    ....


    return cell;
}
ICL1901
  • 7,632
  • 14
  • 90
  • 138
Maddy B
  • 51
  • 1
  • 6
3

Anybody trying to do this in swift then here is my code. Tiny and superb.

This trick works with even static cells where I don't use the cellForRowAtIndexPath method.

Sorry for little answer. Hope you know about datasource an delegates.

 override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    
    if (indexPath.row % 2) != 0{
        cell.backgroundColor = UIColor .blackColor()
    }else{
        cell.backgroundColor = UIColor .lightGrayColor()

    }

 }

It will give you the result like...

Color each alternate cell in ios using Swift

Nimantha
  • 6,405
  • 6
  • 28
  • 69
onCompletion
  • 6,500
  • 4
  • 28
  • 37
2
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

 
static NSString *CellIdentifier = @"Cell";
    
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
    if(indexPath.row%2==0)
        cell.contentView. backgroundColor=[UIColor redColor];
    else
        cell.contentView.backgroundColor=[UIColor greenColor];

        


return cell;


}
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Balu
  • 8,470
  • 2
  • 24
  • 41
  • Thanks for reply! There are so many files in it.so i can't able to find the method for change the code. If you ever used this control, then please tell me the file name in which i will change the code. – user1673099 Mar 19 '13 at 11:08
1

Search for cellForRowAtIndexPath in your code. that method is responsible for creating cell in your tableview. for more info : UITableViewCell with alternate background color in customized cells

Community
  • 1
  • 1
JiteshW
  • 2,195
  • 4
  • 32
  • 61
1

In your cellForRow method you should check if indexPath.row is divisible by 2 assign first color, else assign second color and return the resulting cell

Nimantha
  • 6,405
  • 6
  • 28
  • 69
abdus.me
  • 1,819
  • 22
  • 34
1
func colorForIndex(index: Int) -> UIColor 
{
    let itemCount = stnRepos.count - 1
    let color = (CGFloat(index) / CGFloat(itemCount)) * 0.6
    return UIColor(red: 0.80, green: color, blue: 0.0, alpha: 1.0)
}

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,
    forRowAtIndexPath indexPath: NSIndexPath) 
{        
    if (indexPath.row % 2 == 0)
    {
        cell.backgroundColor = colorForIndex(indexPath.row)
    } else {
        cell.backgroundColor = UIColor.whiteColor()()
    }
}
Alvin George
  • 14,148
  • 92
  • 64
1

For Swift 3 + and with reduced opacity:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

        if(indexPath.row % 2 == 0) {
            cell.backgroundColor = UIColor.red.withAlphaComponent(0.05)
        } else {
            cell.backgroundColor = UIColor.white
        }
}
ICL1901
  • 7,632
  • 14
  • 90
  • 138
1

In such a simple example I would go for ternary operator. I just hate to see the cell.backgroundColor duplicated.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

.
.
.
    cell.backgroundColor = indexPath.row % 2 == 0 ? UIColor.red.withAlphaComponent(0.05) : .white
.
.
.
}
ha100
  • 1,563
  • 1
  • 21
  • 28
0

You can run a loop which will increment the index path.row by 2 and then in the body of that loop you can assign the color.,

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Kasaname
  • 1,491
  • 11
  • 15
0

This is working code for Swift 4.x & Above

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if(indexPath.row % 2 == 0) {
        cell.backgroundColor = UIColor.white
    } else {
        cell.backgroundColor =  UIColor.lightGray
    }
}
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Ashu
  • 3,373
  • 38
  • 34