Uitableview NSDate正在破坏我的应用程序,我不知道为什么
基本内容如下: 我需要用1个UIDatePicker控制2次(NSDate)。我在XCode(V4.4.1)中创建了一个新的单视图项目,只是为了开始处理我需要的项目。我在项目的创建中使用了一个故事板,easy peasy。在故事板中,我去掉了视图控制器,并拖出了一个导航控制器。删除了作为根视图控制器的表视图,并添加了常规视图控制器作为根视图控制器。我在上面放了一个按钮。我拖出另一个视图控制器,并使用“推”将根视图控制器上的按钮连接到这个新的视图控制器。很简单。我测试它,它工作,基本 我为刚刚添加的新视图控制器创建了一个类,并将序列图像板中的视图控制器设置为该类型。在故事板中,我添加了一个UIDatePicker、一个UIButton和一个UITableView(请记住,这只是我在努力使事情正常运行,并尝试学习如何使用所有东西,这还是一个全新的东西) 我的ViewController类的代码如下所示: NewViewController.hUitableview NSDate正在破坏我的应用程序,我不知道为什么,uitableview,ios5,crash,nsdate,uidatepicker,Uitableview,Ios5,Crash,Nsdate,Uidatepicker,基本内容如下: 我需要用1个UIDatePicker控制2次(NSDate)。我在XCode(V4.4.1)中创建了一个新的单视图项目,只是为了开始处理我需要的项目。我在项目的创建中使用了一个故事板,easy peasy。在故事板中,我去掉了视图控制器,并拖出了一个导航控制器。删除了作为根视图控制器的表视图,并添加了常规视图控制器作为根视图控制器。我在上面放了一个按钮。我拖出另一个视图控制器,并使用“推”将根视图控制器上的按钮连接到这个新的视图控制器。很简单。我测试它,它工作,基本 我为刚刚添加
#import <UIKit/UIKit.h>
@interface Reminders : UIViewController <UITableViewDataSource, UITableViewDelegate> {
NSInteger selectedRow;
}
@property (nonatomic, retain) NSDate *amTime;
@property (nonatomic, retain) NSDate *pmTime;
@property (nonatomic, retain) IBOutlet UITableView *tbl;
@property (nonatomic, retain) IBOutlet UIDatePicker *picker;
- (IBAction)timeChange:(id)sender;
- (IBAction)pickerChange:(id)sender;
- (NSString *)dateAsString: (NSDate *)date;
@end
是的,我完全意识到按钮和日期选择器都有改变时间的动作,按钮和相应动作出现的原因是因为我在无法让它按我认为应该的方式工作时开始添加东西
无论如何。时间一开始似乎很好,但从我的观察中可以看出,有些点是无效的objective-c对象。当它试图将“amTime”放入单元格详细信息文本时,它阻塞了CellForRowatineXpath方法
这是踢球的人。。。。我在另一台机器上(具有正确的代码签名密钥的机器)使用上面提到的相同步骤创建了相同的应用程序,并且该版本工作正常,即使我在当前机器上运行该项目。当我在当前机器上创建项目时,它会崩溃
我为这篇文章的篇幅感到抱歉,但我想尽量提供更多的细节。看来您的变量分配得太早了。我将
amTime
和pmTime
保存在实例变量中,并通过self.amTime
和self.pmTime
在我的代码中访问它们:因为它们是属性,所以getter/setter方法将适当地保留/释放它们
raywenderlich.com上有一个精彩的三部分教程,介绍了iOS开发中内存管理的基础知识:必读
ARD做了这个把戏,我没有付出太多的努力,但没有ARC,这只是在amTime和pmTime变量上添加一些保留的问题。我只是坐下来,按照我描述的步骤进行操作,但这次在创建项目时,我选中了“使用自动引用计数”选项,结果成功了。我很高兴它能工作,但很好奇在没有选中该选项的情况下,我到底出了什么问题。自动引用计数(ARC)与内存管理有关,但编译器不必自己处理它,而是通过自动引用计数为您处理它。
#import "Reminders.h"
@interface Reminders ()
@end
@implementation Reminders
@synthesize tbl, picker;
@synthesize amTime, pmTime;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
NSLog(@"init");
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(@"did load");
amTime = [NSDate date];
NSLog(@"AM time viewDidLoad: %@", [self dateAsString:amTime]);
pmTime = [NSDate date];
NSLog(@"PM time viewDidLoad: %@", [self dateAsString:pmTime]);
selectedRow = (NSInteger)1;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//NSLog(@"cell for row");
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"MyIdentifier"];
}
if (indexPath.row == 0) {
cell.textLabel.text = @"Dose";
cell.detailTextLabel.text = @"120";
}
else if (indexPath.row == 1) {
cell.textLabel.text = @"AM";
//cell.detailTextLabel.text = @"time am";
cell.detailTextLabel.text = [self dateAsString:amTime];
}
else if (indexPath.row == 2) {
cell.textLabel.text = @"PM";
//cell.detailTextLabel.text = @"time pm";
cell.detailTextLabel.text = [self dateAsString:pmTime];
}
if (selectedRow == indexPath.row) {
[tbl selectRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:0] animated:NO scrollPosition:0];
}
return cell;
}
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
selectedRow = 0;
}
else if (indexPath.row == 1) {
selectedRow = 1;
[picker setDate:amTime];
}
else if (indexPath.row == 2) {
selectedRow = 2;
[picker setDate:pmTime];
}
}
- (IBAction)timeChange:(id)sender {
if (selectedRow == 1) {
amTime = [NSDate date];
NSLog(@"time change: %@", [self dateAsString:amTime]);
}
else if (selectedRow == 2) {
pmTime = [NSDate date];
NSLog(@"time change: %@", [self dateAsString:pmTime]);
}
else {
}
[tbl reloadData];
}
- (IBAction)pickerChange:(id)sender {
if (selectedRow == 1) {
amTime = [picker date];
NSLog(@"PICKER time change: %@", [self dateAsString:amTime]);
}
else if (selectedRow == 2) {
pmTime = [picker date];
NSLog(@"PICKER time change: %@", [self dateAsString:pmTime]);
}
else {
NSLog(@"picker does NOTHING");
}
[tbl reloadData];
}
- (NSString *)dateAsString:(NSDate *)date {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *stringFromDate = [formatter stringFromDate:date];
return stringFromDate;
}