如何将UITableViewCellSelectionStyle属性设置为某种自定义颜色? [英] How do I set UITableViewCellSelectionStyle property to some custom color?
问题描述
我们如何使用参考中定义的颜色之外的颜色?
提前感谢。
选择是在构建单元格时设置单元格上的 selectedBackgroundView
。
ie
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString * CellIdentifier = @细胞;
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@SelectedCellBackground.png]]] autorelease];
}
//配置单元格
}
使用的图像应该有一个漂亮的渐变(像默认选择)。如果你只是想要一个平面颜色,你可以使用UIView,而不是 UIImageView
,并将 backgroundColor
设置为颜色
当选择此行时,系统会自动应用此背景。
I am developing an iPhone application, in my table view I wanted custom color for Cell Selection Style, I read the UITableViewCell Class Reference but there are only three constants defined for Selection style (Blue, Gray, None). I saw one application that used a different color than those defined in the reference.
How can we use a color other than those defined in the reference?
Thanks in advance.
The best way to set the selection is to set the selectedBackgroundView
on the cell when you construct it.
i.e.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"SelectedCellBackground.png"]] autorelease];
}
// configure the cell
}
The image used should have a nice gradient (like the default selection). If you just want a flat color, you can use a UIView instead of a UIImageView
and set the backgroundColor
to the color you want.
This background is then automatically applied when the row is selected.
这篇关于如何将UITableViewCellSelectionStyle属性设置为某种自定义颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!