0

I have a UITableView with two sections, section 0 contains 1 row and section 1 contains two rows. My Question how can i set two colors to alternative row.I tried the fallowing way but section0 first row and section1 first row shows same same color.

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

{

static NSString *cellIdentifier=@"cell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if(cell==nil)
    {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
 if(indexPath.row%2==0)
    {
        cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Light.png"]];


    }
    else
    {
        cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Dark.png"]];


    }
return cell;
}
CAMOBAP
  • 5,523
  • 8
  • 58
  • 93
venkat
  • 762
  • 2
  • 8
  • 20

2 Answers2

0

Use tableView:willDisplayCell: and set the background colour/image there.

Michael
  • 1,213
  • 6
  • 9
0

Hope this helps...

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

{

    static NSString *cellIdentifier=@"cell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if(cell==nil)
    {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    int cellCount = 0;
    for(int sections=0;sections<indexPath.section;sections++)
    {
        cellCount = cellCount+[self tableView:self.tableView numberOfRowsInSection:sections];
    }
    cellCount = cellCount + indexPath.row
    if(cellCount%2==0)
    {
        cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Light.png"]];
    }
    else
    {
        cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Dark.png"]];
    }

return cell;
}