0

コンテナー ビューに (サブビューとして) 表示しようとしている PFQueryTableViewController のサブクラスがあります。私の問題は、カスタム セルをテーブルビューに表示できないことです。デバッグで次のことを確認しました。

  • テーブルビューが親ビューに追加されています
  • テーブルビューは、更新するためのデフォルトのプルが含まれているため、PFQueryTableView コントローラーです。
  • 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;
 }
4

3 に答える 3

2

正しい方法は、cellForRowAtIndexPath でカスタム セルを呼び出すことです。

次の 2 つの基本事項を確認します。

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"

IDインスペクターでセルを設定します

ここに画像の説明を入力

于 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

UITableView セルを XIB で設計した場合 (そのように聞こえます)、alloc initパラダイムを使用してオブジェクトを初期化することはできません。以下を使用する必要があります。

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