문제
기본적으로 iPhone 테이블보기에는 세 가지 선택 스타일이 있습니다.
회색 - 파란색 - 없음.
회색이나 파란색이 필요하지 않습니다.
내 관습을 설정하고 싶습니다.
예를 들어 정상적인 상황에서는 셀에 "aaaa.png"배경이 있어야합니다.
& 선택된 셀에는 "bbbbb.png"배경이 있어야합니다.
Cell.backgroundView & Cell.SelectedBackground보기를 적용하려고했습니다. 그러나 작동하지 않습니다.
이것에 대한 아이디어가 있다면, 저를 도와주세요.
저를 도와 주셔서 미리 감사드립니다.
해결책
여기에 세 줄의 코드가 있지만 이미지가 필요하지 않습니다!
UIView *selectedBackgroundViewForCell = [UIView new];
[selectedBackgroundViewForCell setBackgroundColor:[UIColor redColor]];
theCell.selectedBackgroundView = selectedBackgroundViewForCell;
다른 팁
다음은 1 줄의 코드로 수행하는 한 가지 방법입니다.
cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pink.png"]] autorelease];
분명히 이미지를 만들어야합니다.
나는 팔로우를 시도했고 그것은 효과가 있었다.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [NSString stringWithFormat:@"%i",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
XML_FitnessPrograms *t=[arrayPrograms objectAtIndex:indexPath.row];
cell=((indexPath.row%2)==0) ?
[self getCellContentView:CellIdentifier program_name:t.program_name alterNate:NO indexPath:indexPath] :
[self getCellContentView:CellIdentifier program_name:t.program_name alterNate:YES indexPath:indexPath] ;
CGRect a=CGRectMake(8, 0, 300, 44);
UIImageView *aImg=[[UIImageView alloc] initWithFrame:a];
UIImageView *bImg=[[UIImageView alloc] initWithFrame:a];
aImg.image=[UIImage imageNamed:@"white-rect-tab.png"]; //arrow.png
bImg.image=[UIImage imageNamed:@"white-rect-tab-copy.png"];
[aImg setContentMode:UIViewContentModeScaleToFill];
[bImg setContentMode:UIViewContentModeScaleToFill];
cell.backgroundView=aImg;
cell.selectedBackgroundView=bImg;
[aImg release];
[bImg release];
}
return cell;
}
-(UITableViewCell*)getCellContentView:(NSString*)cellIdentifier program_name:(NSString*)program_name alterNate:(BOOL)alterNate indexPath:(NSIndexPath*)indexPath
{
UITableViewCell *cell; UILabel *tmp;
CGRect label1Frame=CGRectMake(5, 0, 260, 44);
cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
cell.frame=CGRectMake(8, 0, 280, 44);
cell.backgroundColor=[UIColor clearColor];
tmp=[[UILabel alloc] initWithFrame:label1Frame];
tmp.textColor=[UIColor colorWithRed:(9.0/255.0) green:(68.0/255) blue:(85.0/255) alpha:1.0];
[tmp setFont:[UIFont fontWithName:@"Arial-BoldMT" size:15]];
tmp.text=program_name;
[tmp setShadowColor:[UIColor lightGrayColor]];
tmp.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:tmp]; [tmp release];
[cell setSelectionStyle:UITableViewCellSelectionStyleGray];
return cell;
}
제휴하지 않습니다 StackOverflow