iphone - 自定义 UITableViewCell 创建两次......让我发疯

标签 iphone uitableview

我创建了一个自定义 UITableViewCell,当按下单元格上的按钮时,我想动态扩展和收缩它。当按下按钮时会调用该方法,但似乎单元格被创建了两次...因此重置状态...我已经为此倾注了好几天尝试不同的事情,但我不知道什么错了...

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

// Create a new Cell if necessary

CategoryCell *cell = (CategoryCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];    

if (cell == nil) {
    cell = [[CategoryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    cell.frame = CGRectMake(0.0, 0.0, 320.0, 67.0);

    NSLog( @"Cell Creation - Row %d", indexPath.row );

    [cell.expandContractButton addTarget:self action:@selector(expandContractButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
}
else {
    NSLog( @"Cell Found - Row %d", indexPath.row );
}

// Setup the cell ...

记录....

2012-03-24 11:44:02.158 Review Writer[13523:fb03] Number of rows 1
2012-03-24 11:44:02.172 Review Writer[13523:fb03] Cell Creation - Row 0
2012-03-24 11:44:02.192 Review Writer[13523:fb03] Row 0 - setting height
2012-03-24 11:44:02.197 Review Writer[13523:fb03] Cell Creation - Row 0

有任何关于为什么会出现这种情况的指示吗?

填充单元格数据的代码

-(void)tableView:(UITableView *)tableView willDisplayCell:(CategoryCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"Setting up cell on Row %d - Expanded - %@", indexPath.row, cell.expanded ? @"YES" : @"NO");

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

Category *cat = [categoryList objectAtIndex:indexPath.row];

cell.categoryLabel.text = cat.category;
cell.ratingLabel.text = cat.overall_rating;

if (cell.expanded == YES)
{
    [cell expandCell];
}
else {
    [cell collapseCell];
}

cell.reviewText.text = cat.review_text;

if([cat.overall_rating isEqualToString:@"Not Rated"]){
    [cell.ratingImage setImage:[UIImage imageNamed: @"0_stars.png"]];
}

if([cat.overall_rating isEqualToString:@"Unsatisfactory"]){
    [cell.ratingImage setImage:[UIImage imageNamed: @"1_stars.png"]];
}

if([cat.overall_rating isEqualToString:@"Needs improvement"]){
    [cell.ratingImage setImage:[UIImage imageNamed: @"2_stars.png"]];
}

if([cat.overall_rating isEqualToString:@"Meets job requirements"]){
    [cell.ratingImage setImage:[UIImage imageNamed: @"3_stars.png"]];
}

if([cat.overall_rating isEqualToString:@"Exceeds job requirements"]){
    [cell.ratingImage setImage:[UIImage imageNamed: @"4_stars.png"]];
}

if([cat.overall_rating isEqualToString:@"Outstanding"]){
    [cell.ratingImage setImage:[UIImage imageNamed: @"5_stars.png"]];
}    

// Put the accessory disclosure button on the cell

cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
}

设置单元格高度的代码...

- (CGFloat) tableView: (UITableView *) tableView heightForRowAtIndexPath: (NSIndexPath *) indexPath
{
// Use the method that you would have created above to get the cell.

CategoryCell *cell = (CategoryCell*)[self tableView:tableView cellForRowAtIndexPath:indexPath];

NSString *text = cell.reviewText.text;

// Note the 30 is a fudge factor... :-) Otherwise it does not include the last line...

CGFloat width = [cell.reviewText frame].size.width - 30;

CGFloat height = [text sizeWithFont:cell.reviewText.font constrainedToSize:CGSizeMake(width, 9999) lineBreakMode:UILineBreakModeWordWrap].height;

NSLog( @"Row %d - setting height - Expanded = %@", indexPath.row, cell.expanded ? @"YES" : @"NO" );

if (cell.expanded == NO)
{
    height = 0;
}

return height + 67; 
}

展开/折叠单元格的代码...

- (IBAction) expandContractButtonPressed:(id) sender{

CategoryCell *clickedCell = (CategoryCell *)[[sender superview] superview];
NSIndexPath *clickedButtonPath = [self.tableView indexPathForCell:clickedCell];

CategoryCell *cell = (CategoryCell*)[self tableView:self.tableView cellForRowAtIndexPath:clickedButtonPath];

NSLog( @"EXPAND / COLLAPSE Row %d Exanded = %@", clickedButtonPath.row, clickedCell.expanded ? @"YES" : @"NO");

if (clickedCell.expanded == YES)
{
    NSLog( @"Collapse" );
    [cell collapseCell];
    clickedCell.expanded = NO;
}
else {
    NSLog( @"Expand" );
    [cell expandCell];
    clickedCell.expanded = YES;
}

NSLog( @"Expanded after = %@", clickedCell.expanded ? @"YES" : @"NO" );

NSArray *indexPaths = [[NSArray alloc] initWithObjects:clickedButtonPath, nil];

[self.tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:NO];
}

最佳答案

这只是一种猜测,因为您只提供了代码的一小部分:也许您有多个部分。您只检查行,而不检查部分,因此可以为每个部分调用代码。

关于iphone - 自定义 UITableViewCell 创建两次......让我发疯,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9851508/

相关文章:

ios - 访问不在屏幕上的单元格的内容会导致 fatal error

arrays - 数组无法正确删除元素 swift 4

iphone - 从 iPod 库获取播放列表 - Objective C

iphone - 短信和电话的 URL Scheme

iPhone - 我如何知道 iOS 的振动是打开还是关闭?

iphone - 在 Objective-C 中分配属性

ios - UITableViewCell中图片的异步加载

objective-c - UITableView 行分隔符对于 iOS 9 中的替代单元格可见

swift - 动态转换调用失败

ios - 在 swift 的动态表格 View 单元格中使用 JSON 响应