2014-01-19 11 views
1

我有一個UITableViewCell具有不知道我是否應該把邏輯中的UITableViewController或在UITableViewCell中

  • 兩個標籤
  • 一個TextView的
  • 一號「接受」按鈕
  • 一個「刪除「按鈕

接受和刪除按鈕基本上都是刪除單元格。接受更改一些數據的狀態,它會顯示在另一個屏幕上,刪除將完全刪除它。

這裏是我的方法來顯示一個tableviewcell

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

    static NSString* cellIdentifier = @"Identifier"; 
    InvitationCell *cell = (InvitationCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 


    // Create a new PFObject Object 
    PFObject *connection = nil; 

    connection = [self.tableData objectAtIndex:[indexPath row]]; 
    PFObject *inviterCodeName = connection[@"Inviter"]; 
    [inviterCodeName fetch]; 

    cell.inviterCodeName = inviterCodeName; // Allows us to use this in the cell 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]; 
    [dateFormatter setLocale:enUSPOSIXLocale]; 
    [dateFormatter setDateFormat:@"EEE, MMM, h:mm a"]; 

    cell.dateLabel.text = [dateFormatter stringFromDate:[connection updatedAt]]; 


    cell.codeName.text = [NSString stringWithFormat:@"Invitee Code Name: %@", inviterCodeName[@"codeName"]]; 
    if (inviterCodeName[@"description"] == nil) { 
     cell.codeNameDescription.text = @""; 
    } else { 
     cell.codeNameDescription.text = [NSString stringWithFormat:@"%@", inviterCodeName[@"description"]]; 
    } 
    return cell; 
} 

什麼我不能確定的是我是否把接受/拒絕在實際的UITableViewCell類的方法,或者如果我把它們具有所有類的UITableView方法?

如果我將它們放在具有所有UITableView方法的類中,如何使用自定義單元格中的兩個按鈕?我是否爲他們製作網點?是否有另一個我需要使用的委託方法?

+0

tableviewcell是一種視圖,它應該只關心用戶正在查看的內容 – amar

+0

代表團是最好的方式。在這裏,MVC最適合使用。 – santhu

回答

3

關於你的第一個問題,我會說以下。不要不要把邏輯放在UITableViewCell內。單元不是控制器,而是視圖。所以,它不應該有邏輯裏面。控制器是正確的地方。

關於它的一個很好的討論可以在UITableViewCell Is Not a Controller中找到。我真的建議閱讀它。

關於第二個討論,您可以採取幾種方法。例如,您可以直接在控制器中註冊操作或使用委託模式。我更喜歡後者,因爲它對我來說更加清楚。但這是個人品味。例如,它允許在執行類似操作的另一個控制器內重用這些單元。

這是一個關於如何實現它的舊討論。但它仍然有效。 Recipes to pass data from UITableViewCell to UITableViewController

希望它有幫助。

+0

我想我需要更多地閱讀代表並創建自己的代碼。這是我想我會採取的道路。我還不熟悉代表。 – envinyater

2

在您的InvitationCell.h文件中添加以下屬性。

@property NSIndexPath *indexPath; 

並添加委託方法爲。

-(void)deleteButtonClickedForIndexPath:(NSIndexPath *)indexPath; 
-(void)acceptButtonClickedForIndexPath:(NSIndexPath *)indexPath; 

在你的tableView控制器,的cellForRowAtIndexPath方法中添加以下代碼

cell.delegate=self; 
cell.indexPath = indexPath; 

行添加委託方法來你的控制器和解決它們的基礎上indexPath值。

相關問題