2011-10-27 74 views
0

我已經使用IB創建了自定義單元格,並將其顯示在tableView上,但未涵蓋整個窗口。 我設置了一個toolbar並給了它一個按鈕,用於切換isEditing屬性和按鈕標題。我也在didSelectRowAtIndexPath中製作了if(!self.editing)使用自定義單元格編輯模式

我收到反饋,當按鈕被擊中時,我處於編輯模式,但我的自定義單元格不顯示左側的刪除標誌。如果我滑動單元格,則出現右邊的刪除按鈕,但是如果我按下該按鈕,則應用程序會崩潰,但稍後我會解決該問題,只是認爲我會這樣說,以防萬一導致您我犯的錯誤。

我讀過,如果我沒有將我的自定義單元格分配給cell.contentview,cellforRowAtIndexPath,它可能會發生它不顯示左手刪除標誌。我嘗試了一下,並得到一個錯誤。

這是我第一次用IB製作定製單元格,所以如果我犯了一個愚蠢的錯誤,請和我一起裸露。

cellForRowAtIndexPath的代碼,在那裏我指定自定義單元格:

static NSString *CellIdentifier = @"CustomCell";  
CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) {   
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];   
    for (id currentObject in topLevelObjects){ 
     if ([currentObject isKindOfClass:[UITableViewCell class]]){ 
      cell = (CustomCell *) currentObject; 
      break; 
     } 
    }   
} 
// some more setup and stuff 
return cell; 

感謝您的幫助!

+0

Okkkaaaaay,對不起,那是非常愚蠢的,但它可以發生.. 由於我的視圖控制器是UIViewController的,不是的UITableViewController的子類,我會需要使編輯按鈕切換self.MyTableView.editing .. – Nareille

回答

0

由於我的視圖控制器是UIViewController的,不是的UITableViewController的子類,我會需要讓編輯按鈕切換這樣的:

- (IBAction)editButtonPressed:(id)sender { 
    if (self.myTableView.isEditing) { 
    self.editButton.title = @"Edit"; 
    self.myTableView.editing = NO; 
    } 
    else{ 
     self.editButton.title = @"Done"; 
     self.myTableView.editing = YES; 
    } 
} 

而不是

- (IBAction)editButtonPressed:(id)sender { 
    if (self.myTableView.isEditing) { 
     self.editButton.title = @"Edit"; 
     self.myTableView.editing = NO;  
    } 
    else{ 
     self.editButton.title = @"Done"; 
     self.myTableView.editing= YES; 
    } 
} 

希望幫助別人太..

0

使自定義單元格創建UITableViewCell的子類。使一個有風景的筆尖和一個出口連接到它:

@interface CustomCell : UITableViewCell 
{ 
    IBOutlet UIView* _cellView; 
} 

覆蓋initWithStyle:reuseIdentifier:方法和加載筆尖那裏。自定義視圖添加到單元格的內容視圖:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) 
    { 
     [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 
     [_cellView setFrame:[[self contentView] bounds]]; 
     [[self contentView] addSubview:_cellView]; 
    } 
    return self; 
} 

然後在cellForRowAtIndexPath代碼,您只需撥打:

if (cell == nil) 
{ 
    cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: CellIdentifier ] autorelease]; 
} 
+0

oO我不知道究竟,這種方式有什麼好處,事情是,它只是打破了我的整個設置 - .-該設置的目的是什麼? – Nareille

相關問題