20 多个小时以来,我一直在尝试正确实现 tableView。我问了几个问题来堆栈溢出,并查看了许多其他问题以及论坛中存在类似问题的问题。我得到的最接近的是一个带有空 tableView 的正在运行的程序。我对目标 c 感到非常沮丧。
有人可以发布一个加载了可变数组的单个表视图的示例,其中包含从文本视图和按钮添加的数据?这是我无用代码的一种变体......它没有运行并出现以下错误:
2013-06-10 11:42:05.939 WIDMIR Sign-In[32878:11303] *** Terminating app due to
uncaught exception 'NSUnknownKeyException', reason: '[<ViewController 0x7517c00>
setValue:forUndefinedKey:]: this class is not key value coding-compliant for
the key add.'
*** First throw call stack: (0x1c93012 0x10d0e7e 0x1d1bfb1 0xb7ce41 0xafe5f8 0xafe0e7
0xb28b58 0x232019 0x10e4663 0x1c8e45a 0x230b1c 0xf57e7 0xf5dc8 0xf5ff8 0xf6232 0x453d5
0x4576f 0x45905 0x4e917 0x231b 0x12157 0x12747 0x1394b 0x24cb5 0x25beb 0x17698 0x1beedf9
0x1beead0 0x1c08bf5 0x1c08962 0x1c39bb6 0x1c38f44 0x1c38e1b 0x1317a 0x14ffc 0x1f5d 0x1e85)
libc++abi.dylib: terminate called throwing an exception (lldb)
。H:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>{
UITableView *tableView;
}
- (IBAction)add:(id)sender;
@property (nonatomic, retain) NSMutableArray *currentNames;
@property (weak, nonatomic) IBOutlet UITextField *name;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
米:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize name;
@synthesize currentNames;
@synthesize tableView = _tableView;
- (void)viewDidLoad
{
self.currentNames = [[NSMutableArray alloc] init];
[super viewDidLoad];
UITableView *tv = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain];
self.tableView = tv;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [currentNames count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [currentNames objectAtIndex:indexPath.row];
return cell;
}
- (IBAction)add:(id)sender {
[currentNames addObject:[name text]];
[tableView reloadData];
name.text=@"";
}
@end