PFQueryTableViewController没有显示自定义单元格

时间:2013-11-09 13:49:13

标签: uitableview parse-platform pfquery

我有一个PFQueryTableViewController的子类,我试图在容器视图中显示(作为子视图)。我的问题是我无法在tableview中显示自定义单元格。我通过调试验证了以下内容:

  • 将表视图添加到父视图
  • tableview是一个PFQueryTableView控制器,因为它包含默认的pull to refresh
  • PFQuery返回正确数量的对象
  • 正在调用CellForRowAtIndexPath方法并迭代正确的次数
  • 来自Parse的正确数据将传递给单元格中的不同标签
  • 标签通过我的UITableViewCell子类中的IBOulets连接。当我尝试访问标签时,它正在访问子类并标记
  • 时正常工作

我正确地在这里工作,除了细胞实际出现!我错过了什么?

这是我的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;
 }

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

如果您在XIB中设计了UITableView单元(听起来就是这样),那么就不能使用alloc init范例来初始化对象。你必须使用:

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

答案 2 :(得分:0)

Swift版本(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
}  
}