当custome视图添加到单元格时更改单元格内容视图
问题描述:
我已经创建了一个uiviewcontroller与xib添加到uitableview单元格中。这里是代码。当custome视图添加到单元格时更改单元格内容视图
@interface CustomeCellHome : UIViewController{
IBOutlet UIImageView *imgViewBack;
// will have number of labels and imageviews.
}
@property (nonatomic, retain) UIImageView *imgViewBack;
@implementation CustomeCellHome
@synthesize imgViewBack;
所有这个IBOutlet连接到xib。
现在我将此添加到我的uitableview单元格。这里是代码。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 150;
}
- (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];
// }
CustomeCellHome *objCell = [[CustomeCellHome alloc] init];
[objCell.view setTag:indexPath.row];
[cell.contentView addSubview:objCell.view];
objCell.imgViewBack.image = [UIImage imageNamed:@"unsel.png"];
[objCell release];
return cell;
}
现在我想改变行选择这个imgViewBack图像。这里是代码。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
CustomeCellHome *objCCell = [[CustomeCellHome alloc] init];
objCCell.view = [cell viewWithTag:indexPath.row];
objCCell.imgViewBack.image = [UIImage imageNamed:@"sel.png"];
}
但我imgViewBack没有改变自己的形象。不要弄错这个问题。对此有何建议?任何建议将不胜感激。
答
如果你必须使用像你这样的viewController(再次可能这不是你所需要的),我可以看到你的代码几个问题。
1)而不是[objCell.view setTag:indexPath.row];
使用[objCell.view setTag:indexPath.row+1];
否则,当indexPath.row = 0(我认为0也是超级视图的标签)时你会遇到问题。
2)不要释放viewController,如果你需要保持它。
(但你需要将它保存一些数据结构,以便以后u能释放...)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CellIdentifier";
UITableViewCell *cell = nil;
// u will have problems reusing those cells...
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// if(cell == nil)
// {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
CustomeCellHome *objCell = [[CustomeCellHome alloc] init];
[objCell.view setTag:indexPath.row+1];
[cell.contentView addSubview:objCell.view];
objCell.imgViewBack.image = [UIImage imageNamed:@"image1.png"];
//[objCell release]; // don't release now, add it to some data structure so u can release later
// }
return cell;
}
2)然后在didSelectRowAtIndexPath
获取细胞的的viewController和改变他imgViewBack
财产。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// get the cell
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
// get the cell's viewController
UIView *aView = [cell.contentView viewWithTag:indexPath.row+1];
CustomeCellHome *objCCell = (CustomeCellHome *)[aView nextResponder];
// update the image
objCCell.imgViewBack.image = [UIImage imageNamed:@"image2.png"];
}
这将改变形象,但是这不是一个完美的解决方案,你也会有问题,如果u [R重用细胞...尝试重新思考如果u真的需要使用的viewController。
什么是崩溃错误? – iremk 2012-07-16 07:01:46
和你需要做的我猜, CustomeCellHome * objCCell = [[CustomeCellHome alloc] init]; ,因为它没有初始化.. – iremk 2012-07-16 07:02:40
感谢您回答iremk,我已经试过这个。现在它不会崩溃,但不会改变imageview的图像。我更新了代码。 – 2012-07-16 07:06:28