2012-03-24 6 views
3

セル上でボタンが押されたときに動的に伸縮したいカスタムUITableViewCellを作成しました。このメソッドは、ボタンが押されたときに呼び出されますが、セルが2回作成されているように見えます。したがって、状態をリセットしています...私は別のものを試している間にこれを注いでいますが、このようなケースだろう理由として任意のポインタが間違っている...カスタムUITableViewCell作成2回...私を怒らせる

- (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]; 
} 
+1

私は、おそらく何とか 'reloadData'やその他の__reload__メッセージをUITableViewに2回送ると思いますか? – Tony

+0

CategoryCellには.xibファイルが付属していますか?このクラスを投稿してください。あなたが投稿したコードは、この問題を特定するのに十分ではありませんでした。 –

+0

CategoryCellはXIBで起動しましたが、プログラムで作成されたものに変換されました... – rs2000

答えて

0

コードのごく一部しか提供していないため、おそらく1つのセクションしかありません。行のみをチェックし、セクションはチェックしないので、各セクションに対してコードを呼び出すことができます。

+0

セクションが1つしかありません... – rs2000

0

しかし、あなたのコードではこのような状況(複数の作成)を処理し、セルを正しく表示できるはずです。

セルの高さと内容を設定するコードは表示されませんでした。

あなたはo.kです。 cellForRowAtIndexPath: にセル作成コードのみを保存し、すべてのセル設定コードをwillDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPathに移動する場合は、

+0

ポインタのおかげで...上記のようにセルの内容を設定するコードを移動しました。上記の私のポストに私のコードの他の部分も入れました... – rs2000

+0

NSLogsからもう少し長いログを投稿できますか?ために。セルを展開/折りたたんだときの例 –

関連する問題