1

カスタムTableViewにカスタムセルが動的に入力されています。各セルには、画像と2つのラベルが付いた2つのボタンが含まれています。

テーブルビューをスクロールして戻ると、ボタンの画像が変更されます。これは、カスタムセルの.hファイルのコードです。

class @interface CatalogCategoriesDetailsCell : UITableViewCell
@property (nonatomic, weak) IBOutlet UIButton * firstStickerImageButton;
@property (nonatomic, weak) IBOutlet UIButton *secondStickerImageButton;
@property (strong, nonatomic) IBOutlet UILabel * firstStickerName;
@property (strong, nonatomic) IBOutlet UILabel * secondStickerName;
@end

.mファイル

@implementation CatalogCategoriesDetailsCell
@synthesize firstStickerImageButton = _firstStickerImageButton;
@synthesize secondStickerImageButton = _secondStickerImageButton;
@synthesize firstStickerName = _firstStickerName;
@synthesize secondStickerName = _secondStickerName;
@end

私のTableViewControllerClass.hファイル

#import "CatalogCategoriesStickerDetails.h"

@interface CatalogCategoriesDetails : UITableViewController
{
    TagSingleton *sobj;
}

@property (strong,nonatomic) NSArray *categoryDetails;
@property (strong,nonatomic) NSString *selectedCategoryId;
@property (strong,nonatomic) NSMutableArray *categoryFileNames;
@property (strong, nonatomic) NSString *documentsDirectory;

@end

.mファイル

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [self.categoryFileNames count] / 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    
    static NSString *CellIdentifier = @"categoryDetailsTableCell";

    CatalogCategoriesDetailsCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {

        cell = [[CatalogCategoriesDetailsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...
    NSString *stickerFilePath = [self.documentsDirectory stringByAppendingString:@"/"];

    //first column
    if(self.stickerIndex < [self.categoryFileNames count])
    {
        NSString *stickerFileName = [self.categoryFileNames objectAtIndex:self.stickerIndex];
        //NSLog(@"file name is %@", stickerFileName);
        if(stickerFileName != @"end")
        {
            NSString *stickerFullFilePath = [stickerFilePath stringByAppendingString:stickerFileName];
            UIImage *Image = [UIImage imageWithContentsOfFile:stickerFullFilePath];

            [cell.firstStickerImageButton  setImage:Image forState:UIControlStateNormal];
            [cell.firstStickerImageButton setTag:self.stickerIndex]; 
            [cell.firstStickerImageButton addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];

            NSLog(@"Tag is: %i",cell.firstStickerImageButton.tag);
            //NSLog(@"filename is: %@",stickerFileName);
            NSArray *stickerFileNameCoponents = [stickerFileName componentsSeparatedByString:@"_"]; 
            NSString *stickerName = [stickerFileNameCoponents objectAtIndex: 1]; 
            cell.firstStickerName.text = [cell.firstStickerName.text stringByAppendingString:stickerName];   
        }
    }
    //second column
    if(self.stickerIndex < [self.categoryFileNames count] - 1)
    {
        NSString *stickerFileName = [self.categoryFileNames objectAtIndex:self.stickerIndex + 1];
        //NSLog(@"file name is %@", stickerFileName);
        if(stickerFileName != @"end")
        {
            NSString *stickerFullFilePath = [stickerFilePath stringByAppendingString:stickerFileName];
            UIImage *Image = [UIImage imageWithContentsOfFile:stickerFullFilePath];

            [cell.secondStickerImageButton  setImage:Image forState:UIControlStateNormal];
            [cell.secondStickerImageButton setTag:self.stickerIndex + 1];

            [cell.secondStickerImageButton addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];

            NSLog(@"Tag is: %i",cell.secondStickerImageButton.tag);
            //NSLog(@"filename is: %@",stickerFileName);
            NSArray *stickerFileNameCoponents = [stickerFileName componentsSeparatedByString:@"_"]; 
            NSLog(@"Sticker file name is: %@", stickerFileNameCoponents);
            NSString *stickerName = [stickerFileNameCoponents objectAtIndex: 1]; 
            cell.secondStickerName.text = [cell.secondStickerName.text stringByAppendingString:stickerName];            
        }
        else
        {
            [cell.secondStickerImageButton setHidden:TRUE];
            [cell.secondStickerName setHidden:TRUE];
        }
    }
    self.stickerIndex +=2;
    return cell;
}

-(void)buttonTapped:(UIButton *)button
{
    int tag = button.tag;
    //NSLog(@"Tag is %i", tag);
    sobj = [TagSingleton singleObj];
    sobj.buttonTag = [[NSNumber alloc] initWithInteger:tag];
    [self performSegueWithIdentifier: @"StickerImageSegue" sender: self];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"StickerImageSegue"])
    {        
        CatalogCategoriesStickerDetails *detailViewController = [segue destinationViewController];
        detailViewController.stickerFileNames = self.categoryFileNames;
        detailViewController.documentsDirectory = self.documentsDirectory;        
    }   
}
4

1 に答える 1

0

私は問題を解決しました。問題は、セルを[indexpath行]に接続する代わりに独自のインデックスを使用していたことでした。

于 2013-02-17T16:08:46.900 に答える