如何在iPhone的UITableView中的两个不同部分中为替代行设置两种颜色 [英] how can i set two colors to alternative row in two different sections in UITableView in iphone
本文介绍了如何在iPhone的UITableView中的两个不同部分中为替代行设置两种颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有两个部分的 UITableView
,第 0
部分包含 1
行,第1部分包含两行.我的问题是如何为替代行设置两种颜色.我尝试了休闲方式,但 section0
第一行和 section1
第一行显示相同的颜色.
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;
}
推荐答案
希望这对您有帮助...
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;
}
这篇关于如何在iPhone的UITableView中的两个不同部分中为替代行设置两种颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文