0

我已经创建了一个带有 xib 的 uiviewcontroller 以添加到 uitableview 单元格中。这是代码。

@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 并没有改变它的形象。不明白这有什么问题。有什么建议吗?任何建议将不胜感激。

4

1 回答 1

0

如果您必须像您一样使用 viewController(同样可能这不是您需要的),我可以看到您的代码没有什么问题。

1)当indexPath.row = 0(我认为0也是superview的标签 )时,而不是[objCell.view setTag:indexPath.row];使用否则你会遇到问题。[objCell.view setTag:indexPath.row+1];

2)不要释放viewController,如果你需要一个保持它。
(但你需要将它保存在一些数据结构中,以便稍后你可以释放它......)

- (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"];
}  

这会改变图像,但这不是一个完美的解决方案,如果你重复使用单元格,你会遇到问题......如果你真的需要使用 viewController,请尝试重新考虑。

于 2012-07-16T12:08:42.123 回答