4

基本的に、nib ファイルで作成したカスタム UITableViewCell があります。その中に、プログラムで編集できるようにしたいラベルなどがあります。nibファイルを「post」と呼び、そのファイル所有者を「post」にし、そのクラスをファイル所有者の下でも「post」に設定しました。nib 内のすべてのラベルをこのクラスにリンクしました。

post.h:

#import <Foundation/Foundation.h>

@interface post : UITableViewCell

@property (nonatomic, weak) IBOutlet UILabel *title;
@property (nonatomic, weak) IBOutlet UILabel *authorComments;
@property (nonatomic, weak) IBOutlet UILabel *time;

@end

post.m:

#import "post.h"

@implementation post

    @synthesize title = _title;
    @synthesize authorComments = _authorComments;
    @synthesize time = _time;

@end

私のnibファイルの画像:

ここに画像の説明を入力

したがって、実際のセル自体を除いて、nibファイルのすべてがpostクラスにリンクされています。これは、「ビュー」にリンクする必要があると言われましたが、方法がわかりません(backgroundViewにリンクしようとしましたが、これは私の問題は解決しません)。また、実際のセル オブジェクトにクラス ポストを与えようとしましたが、問題は解決しません。

次に、コントローラーに次のコードがあります。

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

    // this is the identifier that I gave the table cell within my nib
    static NSString *simpleTableIdentifier = @"post";

    // grabs me a cell to use
    post *cell = (post *) [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil) {
        cell = [[post alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    // sets the text on one of the labels within my cell to something else
    cell.title.text = @"this is a test";

    // returns my customized cell
    return cell;

}

ただし、アプリを実行すると、次のようになります。

ここに画像の説明を入力

私が抱えている問題は、カスタムセルに関係していることを知っています。なぜなら、以前にペン先のファイル所有者をコントローラーに設定したときに、カスタムセルが機能していたからです。 UITableViewCell のサブクラスを作成します。私がやりたいのは、複数の異なるラベルを付けたカスタム セルを作成し、セルのテキストをプログラムで編集できるようにすることだけです。

それが役立つ場合は、すべてのソース ファイルと nib ファイルを次に示します。

https://dl.dropboxusercontent.com/u/7822837/source.zip

4

3 に答える 3

10

次のように、UITableView に Nib:forCellReuseIdentifier: を登録する必要があります。

- (void)viewDidLoad {
   [super viewDidLoad];
    [self.tableView registerNib:[UINib nibWithNibName:@"post" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"post"];
}

迅速:

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.registerNib(UINib(nibName: "post", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: "post")
}
于 2013-04-26T05:12:10.400 に答える
5

あなたは2つのことをしなければなりません

xib ファイル内

ファイル所有者のすべてのアウトレットを切断し、アウトレットをカスタムセルオブジェクトに接続します

ここに画像の説明を入力

コード内

ペン先からセルをロードする

if (cell == nil) {

    //cell = [[post alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    cell = [[[NSBundle mainBundle]loadNibNamed:@"post" owner:self options:nil]objectAtIndex:0];
}
于 2013-04-26T04:59:59.070 に答える