当我尝试写入自定义单元格中的标签时,我一直遇到问题。所以现在,我有一个带有名为“AccountViewController”的表格视图的类,我有一个带有自定义单元格“AccountViewCell”的类。我可以让我的单元格正确显示在我的表格中,但是当我尝试时遇到问题将我的自定义单元格中的标签链接到他们的网点以及当我写入标签时。
现在我的代码是:
AccountViewController.h
#import <UIKit/UIKit.h>
#import <GameKit/GKSession.h>
#import <GameKit/GKPeerPickerController.h>
@interface AccountViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
NSArray *tableData;
int cellValue;
NSString *cellContents;
}
@property (nonatomic, retain) NSArray *tableData;
@property (nonatomic, retain) NSString *cellContents;
@property (nonatomic, retain) NSString *accountSelected;
@property (nonatomic, retain) NSString *accountList;
@property (nonatomic, retain) NSString *amount;
@end
AccountViewController.m
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = @"AccountViewCell";
AccountViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
// Load the top-level objects from the custom cell XIB.
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"AccountViewCell" owner:self options:nil];
// Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
cell = [topLevelObjects objectAtIndex:0];
}
// cell.accountNameLabel.text = [tableData objectAtIndex:indexPath.row];
return cell;
}
AccountViewCell.h:
#import <UIKit/UIKit.h>
@interface AccountViewCell : UITableViewCell
@property (nonatomic, weak) IBOutlet UILabel *accountNameLabel;
@property (nonatomic, weak) IBOutlet UILabel *accountNumberLabel;
@property (nonatomic, weak) IBOutlet UILabel *balanceLabel;
@end
AccountViewCell.m
#import "AccountViewCell.h"
@implementation AccountViewCell
@synthesize accountNameLabel = _accountNameLabel;
@synthesize accountNumberLabel = _accountNumberLabel;
@synthesize balanceLabel = _balanceLabel;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
同样,当我不尝试设置标签的文本或将插座连接到 AccountViewCell 中的标签时,我的代码工作正常并显示我的自定义单元格,如下所示:
http://i.imgur.com/TuFun5y.png
我的单元格标识符是正确的:http: //i.imgur.com/ZbsDvaa.png
现在,问题是当我像这样连接插座时:http: //imgur.com/OOiKpkM
即使我没有在 AccountViewController.m 中设置或声明标签,代码也会中断。它给了我这个错误:
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<AccountViewController 0x104b2220> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key accountNameLabel.'
*** First throw call stack:
(0x248e012 0x1d2be7e 0x2516fb1 0x815711 0x796ec8 0x7969b7 0x7c1428 0xd620cc 0x1d3f663 0x248945a 0xd60bcf 0xd6298d 0x28e54 0xbfff4b 0xc0001f 0xbe880b 0xbf919b 0xb9592d 0x1d3f6b0 0x287ffc0 0x287433c 0x2874150 0x27f20bc 0x27f3227 0x27f38e2 0x2456afe 0x2456a3d 0x24347c2 0x2433f44 0x2433e1b 0x29e37e3 0x29e3668 0xb4565c 0x49b2 0x2c45)
libc++abi.dylib: terminate called throwing an exception
我需要一种能够写入标签的方法。我已经尝试了我能想到的一切并阅读了许多线程,但是我无法弄清楚这一点。如果有人可以请帮助并可以向我展示一种在自定义单元格中写入标签的方法,那就太好了。