基本的に、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 ファイルを次に示します。