繁体   English   中英

如何在iPhone的UITableView中的两个不同部分中为替代行设置两种颜色

[英]how can i set two colors to alternative row in two different sections in UITableView in iphone

我有一个包含两部分的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;
}

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

希望这可以帮助...

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM