![](/img/trans.png)
[英]How can i customize UITableView for two columns that have different background colors?
[英]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.