0

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

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

我在这里一切正常,除了单元格实际出现了!我错过了什么?

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

3 回答 3

2

正确的方法是调用cellForRowAtIndexPath中的自定义单元格。

检查两个基本的东西:

1. 在故事板上单击属性检查器中的单元格,检查单元格是否具有正确的标识符

在此处输入图像描述

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"

并在身份检查器中设置单元格

在此处输入图像描述

于 2013-11-09T21:52:37.960 回答
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
}  
}
于 2015-02-20T06:16:18.957 回答
0

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

cell = [[[NSBundle mainBundle] loadNibNamed:@"MyCellXibFile" 
                                     owner:nil 
                                   options:nil] objectAtIndex:0]
于 2013-11-10T04:18:58.270 回答