답변:
여러 가지 방법으로 하이라이트 색상을 변경할 수 있습니다.
셀의 selectionStyle 속성을 변경하십시오. 로 변경하면 UITableViewCellSelectionStyleGray회색으로 표시됩니다.
selectedBackgroundView속성을 변경하십시오 . 실제로 파란색 그라디언트를 만드는 것은보기입니다. 뷰를 생성하고 원하는 것을 그릴 수 있으며 뷰를 테이블 뷰 셀의 배경으로 사용할 수 있습니다.
selectedBackgroundView습니다. 왜냐하면 평범하지 않을 것입니다. "선택"을 기본값으로 설정 한 후에 만 selectedBackgroundView표시됩니다. iOS 6 및 7에서 테스트되었습니다.
cell.selectedBackgroundView = [UIView new];원하는 색상을 설정하십시오.cell.selectedBackgroundView.backgroundColor = [UIColor colorWithHex:@"ecf2f5" andAlpha:1];
Zonble은 이미 훌륭한 답변을 제공했습니다. UIView선택한 배경보기로 표시되는 테이블 뷰 셀에 a 를 추가하기 위해 짧은 코드 스 니펫을 포함하는 것이 유용 할 수 있다고 생각했습니다 .
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
UIView *selectionColor = [[UIView alloc] init];
selectionColor.backgroundColor = [UIColor colorWithRed:(245/255.0) green:(245/255.0) blue:(245/255.0) alpha:1];
cell.selectedBackgroundView = selectionColor;
UITableViewCellselectedBackgroundView을 UIView선택한 배경색으로 만든 셀로 설정했습니다.이것은 나를 위해 잘 작동했습니다. Zonble 팁을 주셔서 감사합니다.
UITableViewCell 세 가지 기본 선택 스타일이 있습니다 :-
구현은 다음과 같습니다 :-
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath {
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
Swift에서 이것을 사용하십시오. cellForRowAtIndexPath
let selectedView = UIView()
selectedView.backgroundColor = .white
cell.selectedBackgroundView = selectedView
선택 색상을 모두 동일하게하려면에서 UITableViewCell사용하십시오 AppDelegate.
let selectedView = UIView()
selectedView.backgroundColor = .white
UITableViewCell.appearance().selectedBackgroundView = selectedView
앱 전체를 변경하려는 경우 앱 위임에 로직을 추가 할 수 있습니다.
class AppDelegate: UIResponder, UIApplicationDelegate {
//... truncated
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
// set up your background color view
let colorView = UIView()
colorView.backgroundColor = UIColor.yellowColor()
// use UITableViewCell.appearance() to configure
// the default appearance of all UITableViewCells in your app
UITableViewCell.appearance().selectedBackgroundView = colorView
return true
}
//... truncated
}
완전성을 위해 : 자신의 하위 클래스를 만든 경우 메소드를 UITableViewCell구현 - (void)setSelected:(BOOL)selected animated:(BOOL)animated하고 컨텐츠보기에 추가 한 일부보기의 배경색을 설정할 수 있습니다. (이 경우) 또는 contentView 자체 (자체보기 중 하나에 포함되지 않은 경우)
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
if(selected) {
self.contentView.backgroundColor = UIColor.blueColor;
} else {
self.contentView.backgroundColor = UIColor.whiteColor;
}
}
(소스 코드 DIV의 작은 너비에 맞추기 위해?를 사용하지 않았습니다.)
이 접근법은 selectedBackgroundView를 사용하는 것보다 두 가지 장점이 있습니다. 메모리가 적고 CPU가 약간 적기 때문에 수백 개의 셀을 표시하지 않으면 눈치 채지 못할 것입니다.
selectedBackgroundView add 메소드의 맨 처음에 할당 된 후에 만 작동 합니다.self. selectedBackgroundView = [UIView new];
selectedBackgroundView속성을 설정하지 않고 iOS 12에서 작동 합니다.
UITableViewCellSelectionStyleDefault사용자 정의 배경색이 작동 하도록 선택 스타일을 설정 해야합니다. 다른 스타일이면 사용자 정의 배경색이 무시됩니다. iOS 8에서 테스트되었습니다.
셀의 전체 코드는 다음과 같습니다.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"MyCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// This is how you change the background color
cell.selectionStyle = UITableViewCellSelectionStyleDefault;
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [UIColor redColor];
[cell setSelectedBackgroundView:bgColorView];
return cell;
}
를 기반으로 @ 사용자의 대답 , 당신은 당신의 응용 프로그램 코드에서이 확장 어느 곳을 추가 할 수 있으며 앱의 모든 세포에 대한 스토리 보드 편집기에서 직접 선택 색상을 가지고 :
@IBDesignable extension UITableViewCell {
@IBInspectable var selectedColor: UIColor? {
set {
if let color = newValue {
selectedBackgroundView = UIView()
selectedBackgroundView!.backgroundColor = color
} else {
selectedBackgroundView = nil
}
}
get {
return selectedBackgroundView?.backgroundColor
}
}
}
@IBDesignable class UIDesignableTableViewCell: UITableViewCell {
@IBInspectable var selectedColor: UIColor = UIColor.clearColor() {
didSet {
selectedBackgroundView = UIView()
selectedBackgroundView?.backgroundColor = selectedColor
}
}
}
스토리 보드에서 UITableViewCell 클래스를 UIDesignableTableViewCell로 설정하고 속성 관리자에서 선택한 셀 색상을 원하는 색상으로 변경할 수 있습니다.
모든 셀에 사용할 수 있습니다. 이것이 속성 관리자의 모습입니다.
UIColor.clear은 아닙니다 UIColor.clearColor(). 그것이 Swift의 변경 사항인지 확실하지 않지만, 없이도 작동합니다 @IBDesignable.