문제

내 uitableview 내의 모든 uitableviewcells의 배경 (그리고 아마도 테두리)을 사용자 정의하고 싶습니다. 지금까지 나는이 물건을 사용자 정의 할 수 없었기 때문에 기본값 인 흰색 배경 셀이 많이 있습니다.

iPhone SDK와 함께 할 수있는 방법이 있습니까?

도움이 되었습니까?

해결책

셀 내용의 배경 검색기를 색상으로 설정해야합니다. 액세서리 (예 : 공개 화살표 등)를 사용하는 경우 흰색으로 표시되므로 사용자 지정 버전을 롤 롤아웃해야 할 수도 있습니다.

다른 팁

이 문제를 해결하기 위해 가장 효율적인 방법은 다음과 같습니다. WillDisplayCell Delegate 메소드를 사용하십시오 (셀 .textlabel.text 및/또는 cell.detailTextLabel.Text를 사용할 때도 텍스트 레이블 배경의 흰색 색상을 처리합니다. ) : :

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { ... }

이 대표 방법이라고 불리는 경우 셀의 색상은 사용할 때와 같이 테이블 뷰가 아닌 셀을 통해 제어됩니다.

- (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath { ... }

따라서 셀 대리자 방법의 본문 내에서 다음 코드를 추가하여 셀의 색상을 번갈아 가거나 함수 호출을 사용하여 테이블의 모든 셀을 동일한 색상으로 만듭니다.

if (indexPath.row % 2)
{
    [cell setBackgroundColor:[UIColor colorWithRed:.8 green:.8 blue:1 alpha:1]];
}
else [cell setBackgroundColor:[UIColor clearColor]];

이 솔루션은 제 상황에서 잘 작동했습니다 ...

OS 3.0은 WillDisplayCell 메소드에서 셀의 배경색을 설정하기 때문에 이것은 정말 간단합니다. CellforseAtindexPath에 색상을 설정해서는 안됩니다.

이것은 평범한 스타일과 그룹화 된 스타일 모두에서 작동합니다.

암호:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    cell.backgroundColor = [UIColor redColor];
}

추신 : 여기 WillDisplayCell의 문서 추출물 :

"테이블 뷰는 셀을 사용하기 직전 에이 메시지를 대의원에게 보냅니다. 따라서 대의원은 셀 객체가 표시되기 전에 셀 객체를 사용자 정의 할 수 있습니다.이 방법은 대의원에게 이전에 설정 한 상태 기반 속성을 무시할 수있는 기회를 제공합니다. 선택 및 배경색과 같은 테이블보기. 대의원이 반환 된 후, 테이블보기는 알파 및 프레임 속성 만 설정 한 다음 줄이 들어 오거나 아웃 될 때만 애니메이션을 할 때만 설정합니다. "

이 정보를 찾았습니다 Colionel 의이 게시물. 감사합니다!

내가 지금까지 찾은 가장 좋은 방법은 셀의 배경보기와 셀 하위 뷰의 명확한 배경을 설정하는 것입니다. 물론, 이것은 액세서리의 유무에 관계없이 인덱스 된 스타일의 테이블에서 멋지게 보입니다.

다음은 Cell의 배경이 노란색으로 묶인 샘플입니다.

UIView* backgroundView = [ [ [ UIView alloc ] initWithFrame:CGRectZero ] autorelease ];
backgroundView.backgroundColor = [ UIColor yellowColor ];
cell.backgroundView = backgroundView;
for ( UIView* view in cell.contentView.subviews ) 
{
    view.backgroundColor = [ UIColor clearColor ];
}

이것을 당신에게 넣으십시오. uitableview delegate class 파일 (.m) :

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell  forRowAtIndexPath:(NSIndexPath *)indexPath 
{
    UIColor *color = ((indexPath.row % 2) == 0) ? [UIColor colorWithRed:255.0/255 green:255.0/255 blue:145.0/255 alpha:1] : [UIColor clearColor];
    cell.backgroundColor = color;
}

나는 SEBA와 동의합니다. 나는 RowforIndexPath 대표 방법에 번갈아 가면서 행 색상을 설정하려고했지만 3.2에서 4.2 사이의 일관성없는 결과를 얻었습니다. 다음은 나를 위해 잘 작동했습니다.

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    if ((indexPath.row % 2) == 1) {
        cell.backgroundColor = UIColorFromRGB(0xEDEDED);
        cell.textLabel.backgroundColor = UIColorFromRGB(0xEDEDED);
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
    }
    else
    {
        cell.backgroundColor = [UIColor whiteColor];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
    }

}

모든 다른 솔루션을 시도한 후 다음 방법이 가장 우아한 솔루션입니다. 다음 대표 방법에서 색상 변경 :

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (...){
        cell.backgroundColor = [UIColor blueColor];
    } else {
        cell.backgroundColor = [UIColor whiteColor];
    }
}

vlado.grigorov는 좋은 조언을 가지고 있습니다. 가장 좋은 방법은 배경 뷰를 만들고 그 색상을 제공하여 다른 모든 것을 ClearColor로 설정하는 것입니다. 또한, 나는 그 방식이 색상을 올바르게 지우는 유일한 방법이라고 생각합니다 (그의 샘플을 시도하지만 'YellowColor'대신 'ClearColor'를 설정).

테이블 뷰 셀의 배경을 사용자 정의하면 결국 "전부 또는 전혀"접근 방식이됩니다. 이상하게 보이지 않는 방식으로 콘텐츠 셀의 배경에 사용되는 색상이나 이미지를 변경하는 것은 매우 어렵습니다.

그 이유는 셀이 실제로 뷰의 너비에 걸쳐 있기 때문입니다. 둥근 코너는 드로잉 스타일의 일부일 뿐이며 콘텐츠보기는이 영역에 있습니다.

콘텐츠 셀의 색상을 변경하면 모서리에서 볼 수있는 흰색 비트로 끝납니다. 전체 셀의 색상을 변경하면보기 너비에 걸쳐 색상 블록이 있습니다.

셀을 확실히 사용자 정의 할 수 있지만 처음에는 생각만큼 쉽지는 않습니다.

보기를 만들고 이것을 셀의 배경보기로 설정하십시오.

UIView *lab = [[UIView alloc] initWithFrame:cell.frame];
            [lab setBackgroundColor:[UIColor grayColor]];
            cell.backgroundView = lab;
            [lab release];

N.Berendt의 답변을 확장하려면 - 실제 셀 데이터 객체의 일부 상태를 기반으로 셀 색상을 설정하려면 테이블 셀에 대한 나머지 정보를 구성 할 때 일반적으로이 경우 일반적으로 있습니다. CellforseAtIndexPath 방법, 컨텐츠보기 배경으로 셀 배경색을 설정하기 위해 WillDisplayCell 메소드를 재정의하여이를 수행 할 수 있습니다. 이는 공개 버튼 배경 등의 문제를 해결합니다. 다른 모든 셀 사용자 정의를 수행하는 곳.

따라서 :이 메소드를 테이블 뷰 대의원에 추가하면 :

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    cell.backgroundColor = cell.contentView.backgroundColor;
}

그런 다음 cellforyatindexpath 방법에서 다음을 수행 할 수 있습니다.

if (myCellDataObject.hasSomeStateThatMeansItShouldShowAsBlue) {
    cell.contentView.backgroundColor = [UIColor blueColor];
}

이로 인해 WillDisplayCell 메소드에서 데이터 객체를 다시 검색 해야하는 것을 절약하고 테이블 셀의 맞춤/사용자 정의를 수행하는 두 곳을 절약 할 수 있습니다.

Photoshop 또는 Gimp와 함께 배경으로 사용할 이미지를 만들고 MyImage를 지명하십시오. 그런 다음이 메소드를 TableViewController 클래스에 추가하십시오.

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    UIImage *cellImage = [UIImage imageNamed:@"myimage.png"];//myimage is a 20x50 px with  gradient  color created with gimp
    UIImageView *cellView = [[UIImageView alloc] initWithImage:cellImage];
    cellView.contentMode = UIContentViewModeScaleToFill;
    cell.backgroundView = cellView;
    //set the background label to clear
    cell.titleLabel.backgroundColor= [UIColor clearColor];
}

UitableView를 Custom In Attribute Inspector로 설정 한 경우에도 작동합니다.

내 솔루션은 CellforyatindexPath 이벤트에 다음 코드를 추가하는 것입니다.

UIView *solidColor = [cell viewWithTag:100];
if (!solidColor) {

    solidColor = [[UIView alloc] initWithFrame:cell.bounds];
    solidColor.tag = 100; //Big tag to access the view afterwards
    [cell addSubview:solidColor];
    [cell sendSubviewToBack:solidColor];
    [solidColor release];
}
solidColor.backgroundColor = [UIColor colorWithRed:254.0/255.0
                                             green:233.0/255.0
                                              blue:233.0/255.0
                                             alpha:1.0];

어떤 상황에서도 공개 버튼이 있어도 작동하며, 논리가 CellwillShow 이벤트보다 CellforsionAtIndexPath의 CellforsicatindexPath에서 Color State에서 작용하는 것이 좋습니다.

서브 클래스 uitableviewcell 및 구현에 추가하십시오.

-(void)layoutSubviews
{
    [super layoutSubviews];
    self.backgroundColor = [UIColor blueColor];
}
    UIView *bg = [[UIView alloc] initWithFrame:cell.frame];
    bg.backgroundColor = [UIColor colorWithRed:175.0/255.0 green:220.0/255.0 blue:186.0/255.0 alpha:1]; 
    cell.backgroundView = bg;
    [bg release];

이것은 최신 Xcode에서 작동합니다.

-(UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath {
    cell.backgroundColor = [UIColor grayColor];
}

이 시도:

- (void)tableView:(UITableView *)tableView1 willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [cell setBackgroundColor:[UIColor clearColor]];
    tableView1.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @"Cream.jpg"]];
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top