2014-07-15 43 views
0

我已經實現了用於刪除和歸檔文章(SHCTableViewCell)的「清除」應用程序樣式刷卡。它運作良好。現在我想在單元格上添加更多標籤,並且已經創建了自定義表格視圖單元格形式的xib文件。然後,我將它與我的自定義單元的類(使用滑動)相關聯。我給了xib單元一個標識符。自定義TableViewCell與新關聯的.xib單元格衝突

,在我的cellForRowAtIndexPath添加以下代碼:

if (!cell) 
    { 
     [tableView registerNib:[UINib nibWithNibName:@"SHCTableViewCell_inbox" bundle:nil] forCellReuseIdentifier:CellIdentifier]; 
     cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    } 

嗯,不錯!現在我可以看到我的自定義標籤,但滑動不起作用。 什麼可能導致這場衝突?

InboxViewController.m

. . . 

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

    static NSString *CellIdentifier = @"ContentCell"; 

    SHCTableViewCell_inbox *cell = nil;//[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
    if (!cell) 
    { 
     [tableView registerNib:[UINib nibWithNibName:@"SHCTableViewCell_inbox" bundle:nil] forCellReuseIdentifier:CellIdentifier]; 
     cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    } 
    cell.textLabel.backgroundColor = [UIColor clearColor]; 
    Article* article = [self.articles objectAtIndex:indexPath.row]; 
    cell.blog.text = article.blog; 
    cell.rating.text = [NSString stringWithFormat: @"%ld", (long)article.rating]; 
    cell.title.text = article.title; 
    [cell setBackgroundColor:[UIColor clearColor]]; 
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 

    cell.delegate = self; 
    cell.todoItem = article; 
    return cell; 
} 

. . . 

XIB細胞文件:

enter image description here

編輯: 我認爲這是值得做的手勢識別器。 SHCTableViewCell_inbox具有initWithStyle:顯然,它永遠不會被調用。 下一行代碼位於initWithStyle中,負責手勢識別。 UIGestureRecognizer * recognitionizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan :)]; recognizer.delegate = self; [self addGestureRecognizer:Recognizer];

+0

也許它並不涉及到這個問題,但你應該考慮移動'[的tableView registerNib:[UINib nibWithNibName:@ 「SHCTableViewCell_inbox」 捆綁:無] forCellReuseIdentifier:CellIdentifier];'到'viewDidLoad'。你不應該檢查單元格是否爲'nil',只需調用'cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];''。不要忘記'... forIndexPath:'你的代碼想念它。 – Keenle

+0

嗨,謝謝。我做了這些改變。 – sermilion

+0

「Custom TableViewCell與新關聯的.xib單元衝突」是什麼意思?你收到任何錯誤?或應用剛剛停止響應滑動手勢? – Keenle

回答

1

正如我們在聊天中發現的那樣:在故事板中初始化單元格的代碼駐留在initWithStyle:reuseIdentifier:方法中。在重構到Xib之後,您應該將該代碼移動到awakeFromNib方法,因爲initWithStyle:reuseIdentifier:不會被調用。

所以,你應該添加代碼婁SHCTableViewCell_inbox類:

- (void)awakeFromNib { 
    UIGestureRecognizer* recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)]; 
    recognizer.delegate = self; 
    [self addGestureRecognizer:recognizer]; 

    // any other code that was in initWithStyle:reuseIdentifier: 
} 
相關問題