0

uitableview セルに追加するために、xib を使用して uiviewcontroller を 1 つ作成しました。これがそのコードです。

@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

uのようにviewControllerを使用する必要がある場合(おそらくこれはuが必要とするものではありません)、コードにいくつかの問題があります。

1)[objCell.view setTag:indexPath.row];使用する代わりに[objCell.view setTag:indexPath.row+1];、indexPath.row = 0の場合にuで問題が発生します(0はスーパービューのタグでもあると思います)。

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 に答える