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 row]。

于 2013-02-17T16:08:46.900 回答