2013-11-09 200 views
0

我有一個PFQueryTableViewController的子類,我試圖在容器視圖(作爲子視圖)中顯示。我的問題是,我無法讓自定義單元格顯示在tableview中。我已經通過調試驗證了以下情況:PFQueryTableViewController不顯示自定義單元格

  • 的tableview中被添加到父視圖
  • 的tableview中是一個PFQueryTableView控制器,因爲它包括默認拉刷新
  • 的PFQuery被返回正確的數的對象
  • 所述的cellForRowAtIndexPath方法被調用,並通過
  • 從解析正確的數據被傳遞給不同的標籤在細胞中正確的次數迭代
  • 標籤通過IBOulets連接到我的UITableViewCell的子類中。當我試圖訪問標籤時它正常工作,因爲它訪問的子類和標籤

我有一切工作正確,除了單元格實際顯示了!我錯過了什麼?

這是我的cellForRowAtIndexPath代碼:

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

    static NSString *CellIdentifier = @"RoundCell"; 

    RoundCell* cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[RoundCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    // get string values from Parse 
    NSString * teeString =[object objectForKey:@"roundTee"]; 
    NSString* courseString = [object objectForKey:@"roundCourse"]; 
     NSString * courseString2 = [[courseString stringByAppendingString:@" - "]stringByAppendingString:teeString]; 
    NSString * dateString = [object objectForKey:@"roundDate"]; 
    NSString * scoreString = [object objectForKey:@"roundScore"]; 
    NSString * differentialString = [object objectForKey:@"roundDifferential"]; 


    cell.courseNameCell.text = courseString2; 
    cell.dateCell.text = dateString; 
    cell.scoreCell.text= scoreString; 
    cell.differentialCell.text=differentialString; 
    return cell; 
} 

回答

2

正確的方法是調用自定義單元格中的cellForRowAtIndexPath。

檢查兩個基本的東西:

1.在故事板,然後單擊在屬性檢查器中的細胞會檢查電池是否具有正確的標識

enter image description here

2集cellForRowAtIndexPath以這種方式:

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

CustomCell *cell = (CustomCell *)[self.tableView dequeueReusableCellWithIdentifier:@"YOUR CELL NAME" forIndexPath:indexPath]; 
你的情況嘗試

所以:

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

    CustomCell *cell = (CustomCell *)[self.tableView dequeueReusableCellWithIdentifier:@"YOUR CELL NAME" forIndexPath:indexPath]; 

    NSString * teeString =[object objectForKey:@"roundTee"]; 
    NSString* courseString = [object objectForKey:@"roundCourse"]; 
     NSString * courseString2 = [[courseString stringByAppendingString:@" - "]stringByAppendingString:teeString]; 
    NSString * dateString = [object objectForKey:@"roundDate"]; 
    NSString * scoreString = [object objectForKey:@"roundScore"]; 
    NSString * differentialString = [object objectForKey:@"roundDifferential"]; 


    cell.courseNameCell.text = courseString2; 
    cell.dateCell.text = dateString; 
    cell.scoreCell.text= scoreString; 
    cell.differentialCell.text=differentialString; 
    return cell; 
} 

不要忘了導入自定義單元格的子類在File.m

#import "YourCustomCell.h" 

,並設置單元格中的身份檢查

enter image description here

+0

我是不是正確的方式實例化細胞。你的代碼做了詭計! – tnb121

+0

我很高興我的幫助! :) – kAiN

+0

你不僅幫助,你救了我把我的電腦扔出窗外。再次感謝 – tnb121

0

如果你在XIB中設計了你的UITableView單元格(這聽起來像你一樣),那麼你不能使用alloc init範例初始化您的對象。你必須使用:

cell = [[[NSBundle mainBundle] loadNibNamed:@"MyCellXibFile" 
            owner:nil 
            options:nil] objectAtIndex:0] 
0

斯威夫特版本(前1.2):

import UIKit 

class JPUsersTableViewController: PFQueryTableViewController { 

override init!(style: UITableViewStyle, className: String!) { 
    super.init(style: style, className: className) 
    textKey = "username" 
    pullToRefreshEnabled = true 
    paginationEnabled = true 
    objectsPerPage = 25 
} 

required init(coder aDecoder: NSCoder) { 
    super.init(coder: aDecoder) 
} 

override func viewDidLoad() { 
    super.viewDidLoad() 

    title = "Users" 

    tableView.registerClass(PFTableViewCell.self, forCellReuseIdentifier: kTableViewCellIdentifier) 
    tableView.separatorInset.right = tableView.separatorInset.left 
    tableView.tableFooterView = UIView(frame: CGRectZero) 
    view.backgroundColor = kbackgroundColor 

    let returnIcon = UIBarButtonItem(image: kNavBarReturnIcon, style: .Plain, target: navigationController, action: "popViewControllerAnimated:") 
    returnIcon.tintColor = kToolbarIconColor 
    navigationItem.leftBarButtonItem = returnIcon 

    tableView.reloadData() 
    addPullToRefresh() 
} 

override func queryForTable() -> PFQuery! { 
    let query = PFUser.query() 
    query.whereKey("username", notEqualTo: PFUser.currentUser().username) 
    query.orderByAscending("username") 

    //if network cannot find any data, go to cached (local disk data) 
    if (self.objects.count == 0){ 
     query.cachePolicy = kPFCachePolicyCacheThenNetwork 
    } 

    return query 
} 

// MARK: - Navigation 

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!, object: PFObject!) -> PFTableViewCell! { 
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as PFTableViewCell 
    cell.textLabel?.text = object["username"] as? String 

    if let profileImage = object["profileImage"] as? PFFile { 
     cell.imageView.file = profileImage 
    } 
    else { 
     cell.imageView.image = kProfileDefaultProfileImage 
    } 

    cell.textLabel?.font = UIFont(name: kStandardFontName, size: kStandardFontSize) 
    cell.textLabel?.textColor = UIColor.whiteColor() 
    cell.backgroundColor = kbackgroundColor 

    return cell 
} 

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    return 50 
} 
} 
+0

做這一切,仍然無法得到它的故事板的斯威夫特2工作:http://stackoverflow.com/questions/34986817/cant-display-custom-cell-with-pfquerytableviewcontroller-swift2 – Polis

+0

解析正在關閉down @Polis – ericgu

+0

我知道了!我恨他們。 – Polis

相關問題