2011-12-12 59 views
3

我已經創建了一個按鈕,將行狀態從「availible」更改爲「買過」的自定義單元格,並且我還有一個顯示狀態的UIImageview。當用戶按下按鈕時,我希望單元格重新繪製新的狀態。UITableviewController - 更新特定的單元格

我已經能夠在用戶將單元格滾動出視野後重新繪製UIImage。這是我到目前爲止有:

初始化

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.clearsSelectionOnViewWillAppear = YES; 
    self.contentSizeForViewInPopover = CGSizeMake(600.0, 560.0); 
    //[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]; 
    [self.tableView setRowHeight:60]; 
    [self.tableView setBackgroundColor:[UIColor blackColor]]; 
} 

只拿到一個部分:

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

所有項目都是 「paketListe」 數組:

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

cellForRow函數僅在單元格不在視圖中並且單元格初始繪製時才被調用。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"CellForRow called!"); 
    PaketTableViewCell *cell = [[[PaketTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil] autorelease]; 
    [cell setDelegate:self]; 
    Paket *tempPaket = [paketListe objectAtIndex:[indexPath row]]; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *userDocumentsPath = [paths objectAtIndex:0]; 
    NSString *fileName = [NSString stringWithFormat:@"%@/%@/%@",userDocumentsPath, [tempPaket productID], [tempPaket previewPic]]; 
    [cell setProductIndex:[indexPath row]]; 
    //... Setting some Attributes in my custom cell, cut out for simplicity.... 
    cell.backgroundColor = [UIColor clearColor]; 
    return cell; 
} 

Button的代碼,這是一個委託調用,所以我的自定義銷售調用[委託buttonPushed:(int)];

- (void) buttonPushed:(int) myProductIndex { 
    NSLog(@"Pushed: %@", [[paketListe objectAtIndex:myProductIndex] productID]); 
    CoreDataWrapper *tempCDW = [[CoreDataWrapper alloc] init]; 
    [tempCDW initCoreData]; 
    Paket *tempPaket = [paketListe objectAtIndex:myProductIndex]; 
    //Doing some work in Core Data an my "paketListe" array. Left out for simplicity... 
    [tempCDW release]; 
    //Here it begins.... 
    [[self tableView] reloadData]; 
    [[self tableView] beginUpdates]; 
    [[self tableView] reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:myProductIndex inSection:0]] withRowAnimation:UITableViewRowAnimationFade]; 
    [[self tableView] endUpdates]; 
} 

現在我試着用不同的命令重裝...刷新只有reloadData整個事情或begin-/endUpdate的事情。什麼都不刷新細胞。經過幾個小時的閱讀,我認爲我做的都對,但沒有更新。

我很感激任何幫助。

+0

我假設你嘗試過'[self.tableView setNeedsDisplay]',對吧? – dasblinkenlight

+0

啊,是的,我也試過這個。 –

回答

0

快速和骯髒的:

- (void)configureCell:(PaketTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath { 

    //Any cell-specific code 

    [cell setNeedsLayout]; //or [cell setNeedsDisplay]; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    //Code to create any/a general cell 

    [self configureCell:cell]; 

    return cell; 
} 

- (void) buttonPushed:(int) myProductIndex { 

    //Code to identify the cell in which your button is (use "super") 

    [self configureCell:cellForPushedButton]; 
} 
+0

試過了,但沒有重繪。配置單元格被調用,一切都設置好,但沒有重繪。無論是使用'setNeedsLayout'還是'setNeedsDisply'。現在我很困惑。 –

0

我知道了你的問題,你已經錯過了在reloadRowsAtIndexPaths「無」。 按照我在下面寫的,它會工作。

[self.tableView beginUpdates]; 
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:myProductIndex inSection:0], nil] withRowAnimation:UITableViewRowAnimationNone]; 
[self.tableView endUpdates]; 
相關問題