0

我有UITableView两个部分,部分0包含1行,部分 1 包含两行。我的问题如何将两种颜色设置为替代行。我尝试了休闲方式,但section0第一行和section1第一行显示相同的颜色。

-(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;
}
4

2 回答 2

0

使用 tableView:willDisplayCell: 并在那里设置背景颜色/图像。

于 2012-11-12T09:14:02.697 回答
0

希望这可以帮助...

-(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;
}
于 2012-12-20T12:23:02.147 回答