我有自定义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;
}
}
答案 0 :(得分:0)
我已经解决了问题,问题是我使用自己的索引而不是将单元格连接到[indexpath row]。