[ios] UITableViewCell의 파란색 강조 표시 색상을 변경하는 방법은 무엇입니까?

UITableViewCell아이디어 의 파란색 강조 / 선택 색상을 변경하는 방법이 궁금합니다 .



답변

여러 가지 방법으로 하이라이트 색상을 변경할 수 있습니다.

  1. 셀의 selectionStyle 속성을 변경하십시오. 로 변경하면 UITableViewCellSelectionStyleGray회색으로 표시됩니다.

  2. selectedBackgroundView속성을 변경하십시오 . 실제로 파란색 그라디언트를 만드는 것은보기입니다. 뷰를 생성하고 원하는 것을 그릴 수 있으며 뷰를 테이블 뷰 셀의 배경으로 사용할 수 있습니다.


답변

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;
  • 세포는 나의 UITableViewCell
  • UIView를 만들고 RGB 색상 (밝은 회색)을 사용하여 배경색을 설정했습니다.
  • 그런 다음 셀 selectedBackgroundViewUIView선택한 배경색으로 만든 셀로 설정했습니다.

이것은 나를 위해 잘 작동했습니다. Zonble 팁을 주셔서 감사합니다.


답변

UITableViewCell 세 가지 기본 선택 스타일이 있습니다 :-

  1. 푸른
  2. 회색
  3. 없음

구현은 다음과 같습니다 :-

- (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가 약간 적기 때문에 수백 개의 셀을 표시하지 않으면 눈치 채지 못할 것입니다.


답변

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