我想现在如何在单击特定单元格时在单元格背景上添加图像 我的表分组我有三个部分和5行 我尝试这个代码,但它不适合我 我在两个方法中都写了这个代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
UIImage *rowBackground;
UIImage *selectionBackground;
rowBackground = [UIImage imageNamed:@"topAndBottomRow.png"];
selectionBackground = [UIImage imageNamed:@"topRowSelected.png"];
cell.backgroundView = [[[UIImageView alloc] init] autorelease];
cell.selectedBackgroundView = [[[UIImageView alloc] init] autorelease];
((UIImageView *)cell.backgroundView).image = rowBackground;
((UIImageView *)cell.selectedBackgroundView).image = selectionBackground;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIImage *rowBackground;
UIImage *selectionBackground;
rowBackground = [UIImage imageNamed:@"topAndBottomRow.png"];
selectionBackground = [UIImage imageNamed:@"topRowSelected.png"];
cell.backgroundView = [[[UIImageView alloc] init] autorelease];
cell.selectedBackgroundView = [[[UIImageView alloc] init] autorelease];
((UIImageView *)cell.backgroundView).image = rowBackground;
((UIImageView *)cell.selectedBackgroundView).image = selectionBackground;
}
答案 0 :(得分:1)
试一试: -
- (void)tableView: (UITableView*)tableView willDisplayCell: (UITableViewCell*)cell1 forRowAtIndexPath: (NSIndexPath*)indexPath
{
cell1.backgroundColor = indexPath.row % 2 ? [UIColor colorWithPatternImage:[UIImage imageNamed:(@"back2.png")]]: [UIColor colorWithPatternImage:[UIImage imageNamed:(@"back1.png")]];
UIView *myBackView = [[UIView alloc] initWithFrame:cell1.frame];
myBackView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:(@"back3.png")]];
cell1.selectedBackgroundView = myBackView;
}
它可能会对你有所帮助:))
答案 1 :(得分:0)
这样做
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
UIImage *rowBackground;
UIImage *selectionBackground;
rowBackground = [UIImage imageNamed:@"topAndBottomRow.png"];
selectionBackground = [UIImage imageNamed:@"topRowSelected.png"];
backgroundImageView = [[[UIImageView alloc] init] autorelease];
backgroundImageView.image = rowBackground ;
selectedBackgroundImageView = [[[UIImageView alloc] init] autorelease];
selectedBackgroundImageView.image =selectionBackground ;
cell.backgroundView = backgroundImageView;
cell.selectedBackgroundView = selectedBackgroundImageView;
return cell;
}
不需要在didSelect I Guess中写相同的内容。
iphone objective-c