UITableViewCell은 흰색 배경을 표시하며 iOS7에서 수정할 수 없습니다
에서 상속하는 사용자 정의 테이블 뷰 셀 클래스를 구현했습니다 UITableViewCell
. 테이블 뷰에는 배경 이미지가 포함되어 있으므로 셀의 배경을 투명하게 만들고 싶습니다. iOS7 이전에는 멋지게 보입니다.
그러나 iOS7에서는 셀이 항상 흰색 배경으로 표시됩니다.
Xcode7, 2015 에서도 스토리 보드에 버그 가 있습니다. 코드에서 셀의 배경색을 설정해야합니다.
Apple DOC가 말했듯이 ( UITableViewCell 클래스 참조 ) :
... iOS 7에서 셀은 기본적으로 흰색 배경을 갖습니다 . 이전 버전의 iOS에서는 셀이 둘러싸는 테이블보기의 배경색을 상속합니다. 셀의 배경색을 변경하려면 테이블 뷰 대리자 의 tableView : willDisplayCell : forRowAtIndexPath : 메서드에서 변경하십시오.
따라서 투명한 배경으로 셀을 표시하려면 아래처럼 테이블보기 컨트롤러에서 대리자 메서드를 구현하면됩니다.
- (void)tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
{
[cell setBackgroundColor:[UIColor clearColor]];
}
참고 : @null이 말했듯이 "... 인터페이스 빌더에는 버그가있는 것 같습니다 ..."라고 버그가 있는지 완전히 확신하지 못하지만 그의 의견이 여러 번 투표를 한 것으로 보입니다. 따라서 IB를 사용하면 문제가 발생할 수 있습니다. :)
나는 그것을 조금 조사하고 셀 backgroundColor가 Appearance 시스템에 의해 설정되어 있음을 알았습니다. 따라서 응용 프로그램의 모든 셀에 명확한 배경이 있으면 가장 쉬운 해결책은 다음과 같습니다.
[[UITableViewCell appearance] setBackgroundColor:[UIColor clearColor]];
배경이 다른 경우에도 기본 색상으로 선명한 색상이 가장 편리한 것 같습니다.
셀을 리턴하기 전에 cellForRowAtIndexPath 메소드에이를 작성하십시오.
cell.backgroundColor = [UIColor clearColor];
UITableViewCell
iOS 7에서 의 기본 배경색 은 흰색입니다.
backgroundColor
코드 어딘가에 속성 을 설정 해야합니다. 예를 들어, 셀을 새로 만든 후에 설정하십시오.
cell.backgroundColor = [UIColor clearColor];
실제로 UITableView의 배경색이 명확하게 설정되지 않았기 때문에 문제가 발생했습니다. UITableViewCell의 배경색을 지우고 여전히 흰색으로 표시되는 경우 UITableView의 배경색이 선명하게 (또는 원하는대로) 설정되어 있는지 확인하십시오.
[self.tableView setBackgroundView:nil];
[self.tableView setBackgroundColor:[UIColor clearColor]];
스위프트에서
tableView.backgroundView = nil
tableView.backgroundColor = UIColor.clearColor()
이것은 분명히 iOS 버그입니다. iOS 8에서 배경색을 설정 Interface Builder
하면 iPhone에서 잘 작동하지만 iPad에서는 항상 그렇습니다 whiteColor
. 이를 수정하려면 cellForIndexPath
데이터 소스 메시지에서 다음을 입력하십시오.
cell.backgroundColor = cell.backgroundColor
그리고 작동합니다. 이것이 바로 코드 자체가 거의 엉망이어서 버그라고 말한 이유입니다.하지만 작동합니다.
기본적으로 UITableViewCell이 선택되어있을 수 있습니다. Xcode 6s의 새로운 시각적 디버거를 사용하여이를 확인할 수 있습니다 (또는이 화이트 셀이 나타나는 원인을 정확히 알 수 있음).
흥미롭게도 이것을 알고 난 후에 .. 선택한 셀의 배경색을 지워도 여전히 작동하지 않습니다. 더 많은 연구를 수행하면이 사용자 정의 셀을 만들 때 선택 스타일을 수정해야한다는 것이 밝혀졌습니다.
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// do customization here
}
self.selectionStyle = UITableViewCellSelectionStyleNone;
[self setBackgroundColor:[UIColor clearColor]];
return self;
}
위의 어느 것도 XCode 5.1.1, iOS 7.1에서 작동하지 않는다는 것을 알았습니다.
프로토 타입 셀과 함께 인터페이스 빌더를 사용하는 경우 프로토 타입 셀을 선택한 다음보기 섹션의 속성 선택기에서 배경 양식 기본값을 선명한 색상으로 변경하십시오.
이것은 잘 작동하는 것 같습니다. 위의 코드 변경은 필요하지 않습니다. 순수한 IB 솔루션입니다.
The accepted answer did not fix the problem for me. I had to do this:
- In the interface builder, select the table view. Then from the attributes inspector, from the View section, set the Background to transparent (0% opacity).
From the table's data source class cellForRowAtIndexPath method:
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; cell.backgroundColor = [UIColor clearColor]; //Make cell transparent
For me setting cell.backgroundColor = cell.contentView.backgroundColor;
either in tableView:willDisplayCell:forRowAtIndexPath:
or tableView:cell:forRowAtIndexPath:
did the job.
This is because I set the contentView's background colour in Interface Builder as desired.
When adding UI objects to a cell, Xcode 5/IOS 7 adds a new "Content View" which will be the superview of all elements in the cell. To set the background color of the cell, set the background color of this Content View. The solutions above didnt work for me but this one worked well for me.
Swift 1.2 Solution:
다음과 같이 셀의 배경색에 대한 명시 적 정의를 추가하십시오.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// Configure the cell...
cell.backgroundColor = UIColor.clearColor()
return cell
}
비슷한 문제가 있었어요
- selectionStyle로 파란색을 설정하고
이것을 코드에 추가하여 수정하십시오.
override func tableView(tableView: UITableView, shouldHighlightRowAtIndexPath indexPath: NSIndexPath) -> Bool { var bgColorView: UIView = UIView() bgColorView.backgroundColor = UIColor(red: (76.0 / 255.0), green: (161.0 / 255.0), blue: (255.0 / 255.0), alpha: 1.0) bgColorView.layer.masksToBounds = true tableView.cellForRowAtIndexPath(indexPath)!.selectedBackgroundView = bgColorView return true }
UITableView 셀을 유리하게 만들기 위해 색상 코드를 사용할 수도 있습니다.
[cell setBackgroundColor:[self colorWithHexString:@"1fbbff"]];
다음은 색상 코드 방법을 적용하는 코드입니다.
-(UIColor*)colorWithHexString:(NSString*)hex
{
NSString *cString = [[hex stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
if ([cString length] < 6) return [UIColor grayColor];
if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];
if ([cString length] != 6) return [UIColor grayColor];
NSRange range;
range.location = 0;
range.length = 2;
NSString *rString = [cString substringWithRange:range];
range.location = 2;
NSString *gString = [cString substringWithRange:range];
range.location = 4;
NSString *bString = [cString substringWithRange:range];
unsigned int r, g, b;
[[NSScanner scannerWithString:rString] scanHexInt:&r];
[[NSScanner scannerWithString:gString] scanHexInt:&g];
[[NSScanner scannerWithString:bString] scanHexInt:&b];
return [UIColor colorWithRed:((float) r / 255.0f)
green:((float) g / 255.0f)
blue:((float) b / 255.0f)
alpha:1.0f];
}
'IT' 카테고리의 다른 글
NHibernate ISession Flush : 언제 어디서 사용해야하며 왜 그런가? (0) | 2020.05.16 |
---|---|
정규식은 대문자를 소문자로 바꿉니다. (0) | 2020.05.15 |
scikit-learn에서 여러 열의 레이블 인코딩 (0) | 2020.05.15 |
선택된 항목을 맨 위에 표시하려면 RecyclerView를 스크롤하십시오. (0) | 2020.05.15 |
SimpleXMLElement 객체로부터 가치 얻기 (0) | 2020.05.15 |