【问题标题】:PFQueryTableViewController not showing custom cellsPFQueryTableViewController 不显示自定义单元格
【发布时间】:2013-11-21 12:07:29
【问题描述】:

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

  • tableview 正在添加到父视图中
  • 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;
 }

【问题讨论】:

    标签: uitableview parse-platform pfquery


    【解决方案1】:

    正确的方法是在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"
    

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

    【讨论】:

    • 我很高兴能帮上忙! :)
    • 您不仅帮助了我,还让我免于将电脑扔出窗外。再次感谢
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      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
      }  
      }
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-21
      • 2017-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多