2012-07-05 97 views
0

我对iOS相当陌生,有很多东西需要学习,并希望你们能指导我从我的错误中解脱出来。IOS加载数据表格视图

我最近学会了从TableView传递数据到DetailView,并认为,为什么不是相反。我也开始构建一个StopWatch应用程序,并认为日志功能会非常有用。

就这样说,我目前正在构建一个秒表应用程序,它可以作为定时器使用,并具有高分记录功能。它从View(秒表)到tableView(日志板)我使用一个NSMutableArray作为临时存储来保存信息,因为它们在应用程序启动/关闭时应该丢失。不幸的是,通过在这里和那里跟随和改变变量,我现在感到困惑和困惑。

感谢您的建议和帮助,你们给了我感谢@Abizern给我提示。管理解决所有问题。在这里留下代码以防止任何人在将来做类似的事情。

TimerViewController.h

#import <UIKit/UIKit.h> 
#import "SampleData.h" 
#import "SampleDataDAO.h" 
#import "HighScoreTableViewController.h" 
@interface TimerViewController : UIViewController 
{ 
    NSTimer *stopWatchTimer; // Store the timer that fires after a certain time 
    NSDate *startDate; // Stores the date of the click on the start button 
} 
@property(nonatomic, strong) SampleDataDAO *daoDS; 
@property(nonatomic, strong) NSMutableArray *ds; 

@property (retain, nonatomic) IBOutlet UILabel *stopWatchLabel; 
@property (weak, nonatomic) IBOutlet UIButton *onStartPressed; 
@property (weak, nonatomic) IBOutlet UIButton *onStopPressed; 
@property (weak, nonatomic) IBOutlet UIButton *onLogPressed; 
@property (weak, nonatomic) IBOutlet UIButton *onHighscorePressed; 

- (IBAction)onStartPressed:(id)sender; 
- (IBAction)onStopPressed:(id)sender; 
- (IBAction)onLogPressed:(id)sender; 
- (IBAction)onHighscorePressed:(id)sender; 

@end 

TimerViewController.m

#import "TimerViewController.h" 

@interface TimerViewController() 

@end 

@implementation TimerViewController 
@synthesize stopWatchLabel; 
@synthesize onStartPressed; 
@synthesize onStopPressed; 
@synthesize onLogPressed; 
@synthesize onHighscorePressed; 
@synthesize ds,daoDS; 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    daoDS = [[SampleDataDAO alloc] init]; 
    self.ds = daoDS.PopulateDataSource; 
    onStopPressed.enabled=false; 
} 

- (void)viewDidUnload 
{ 
    [self setStopWatchLabel:nil]; 
    [self setOnStartPressed:nil]; 
    [self setOnLogPressed:nil]; 
    [self setOnStopPressed:nil]; 
    [self setOnHighscorePressed:nil]; 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ 
    if ([[segue identifier] isEqualToString:@"showDetail"]) { 
     HighScoreTableViewController *detailViewController = [segue destinationViewController]; 
     detailViewController.arrayOfSampleData = self.ds; 
    } 
} 



- (void)updateTimer 
{ 
    NSDate *currentDate = [NSDate date]; 
    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate]; 
    NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval]; 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"HH:mm:ss.S"]; 
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]]; 
    NSString *timeString=[dateFormatter stringFromDate:timerDate]; 
    stopWatchLabel.text = timeString; 

} 

- (IBAction)onStartPressed:(id)sender { 
    startDate = [NSDate date]; 

    // Create the stop watch timer that fires every 10 ms 
    stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0 
                 target:self 
                selector:@selector(updateTimer) 
                userInfo:nil 
                repeats:YES]; 
    onStartPressed.enabled=false; 
    onStopPressed.enabled=true; 
} 

- (IBAction)onStopPressed:(id)sender { 
    [stopWatchTimer invalidate]; 
    stopWatchTimer = nil; 
    [self updateTimer]; 
    onStartPressed.enabled=true; 
} 

- (IBAction)onLogPressed:(id)sender { 
    NSString * timeCaptured = stopWatchLabel.text; 

    static NSInteger i = 1 ; 
     SampleData* mydata = [[SampleData alloc]init]; 

     mydata.clueName=[NSString stringWithFormat:@"clue %d",i++ ]; 
     mydata.timeLog = timeCaptured; 
     [self.ds addObject:mydata]; 


     NSLog(@"%@",mydata.clueName); 
     NSLog(@"time %@", mydata.timeLog); 
     NSLog(@"%d",[self.ds count]); 
     mydata=nil; 
    } 

- (IBAction)onHighscorePressed:(id)sender { 
    NSLog(@"Proceeding to HighScore"); 
} 



@end 

HighScoreTableView.h

#import <UIKit/UIKit.h> 
    #import "SampleData.h" 
    #import "SampleDataDAO.h" 
    #import "TimerViewController.h" 
    @interface HighScoreTableViewController : UITableViewController 
    @property (nonatomic, strong) NSMutableArray *arrayOfSampleData; 
    @property (nonatomic, strong) SampleData * highscoreData; 
    @end 


HighScoreTableView.m 

#import "HighScoreTableViewController.h" 

@interface HighScoreTableViewController() 

@end 

@implementation HighScoreTableViewController 
@synthesize highscoreData; 
@synthesize arrayOfSampleData; 
- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 


- (void)viewDidLoad 
{ 
    highscoreData = [[SampleData alloc]init]; 
    [super viewDidLoad]; 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return self.arrayOfSampleData.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"highscoreCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    // Configure the cell... 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    //highscoreData = [self.arrayOfSampleData objectAtIndex:indexPath.row]; 

highscoreData = (SampleData *)[self.arrayOfSampleData objectAtIndex:indexPath.row]; //if above line doesn't work, use this 
cell.textLabel.text=[NSString stringWithFormat:@"%@ time %@",highscoreData.clueName, highscoreData.timeLog]; 
return cell; 
} 


#pragma mark - Table view delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Navigation logic may go here. Create and push another view controller. 
    /* 
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil]; 
    // ... 
    // Pass the selected object to the new view controller. 
    [self.navigationController pushViewController:detailViewController animated:YES]; 
    */ 
} 

@end 

SampleData.h

#import <Foundation/Foundation.h> 

@interface SampleData : NSObject 
@property(nonatomic,strong) NSString * clueName; 
@property(nonatomic,strong) NSString * timeLog; 
@end 

SampleData.m

#import "SampleData.h" 

@implementation SampleData 
@synthesize clueName,timeLog; 
@end 

SampleDataDAO.h

#import <Foundation/Foundation.h> 
#import "SampleData.h" 
@interface SampleDataDAO : NSObject 
@property(nonatomic, strong) NSMutableArray * someDataArray; 

-(NSMutableArray *)PopulateDataSource; 
@end 

SampleDataDAO.m(不知道是否需要此DAO的NSObject)

#import "SampleDataDAO.h" 

@implementation SampleDataDAO 
@synthesize someDataArray; 

-(NSMutableArray *)PopulateDataSource 
{ 
    someDataArray = [[NSMutableArray alloc] init]; 
    SampleData * mydata = [[SampleData alloc] init]; 


    mydata = nil; 


    return someDataArray; 
} 
@end 
+0

您未能在您的问题中定义'''SampleDataDAO'''。 – tomk 2012-07-05 10:28:31

回答

1

有你的编码几个失误:

  1. 你需要使用prepareForSegue从父数据传递到子视图控制器。在你的情况下,从TimerViewControllerHighScoreTableViewController

  2. 在你HighScoreTableViewController类,创建一个实例变量数组,将持有的sampleData数组,你会从TimerViewController即时通过prepareForSeque传了过来。事情是这样的:

HighScoreTableViewController.h

@property (nonatomic, strong) NSArray *arrayOfSampleData; 

3。在您的prepareForSequeTimerViewController中,此行是错误的:

//TimerViewController.highscoreData = [self.ds objectAtIndex:[self.tableView indexPathForSelectedRow] .row];

试试这个:

detailViewController.arrayOfSampleData = self.ds; 

4。在HighScoreTableViewController.m,viewDidLoad中下,更换此

highscoreData = (SampleData *)self.highscoreData; 

有:

highscoreData = [SampleData alloc]init]; 

5。在numberOfRowsInSection中,您现在可以执行此操作:

return self.arrayOfSampleData.count; 

6。在cellForRowAtIndexPath中,

highscoreData = [self.arrayOfSampleData objectAtIndex:indexPath.row]; 

//highscoreData = (SampleData *)[self.arrayOfSampleData objectAtIndex:indexPath.row]; //if above line doesn't work, use this 

cell.textLabel.text = @"%@ time %@ ", highscoreData.clueName, highscoreData.timeLog; 
1

在你HighScoreTableViewController您需要访问数组例如通过声明并确定一个可写的属性:

@property(nonatomic, strong) NSMutableArray *myArr; 

,那么你可以定义

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [self.myArr count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    // ... like in your code 

    // Tried changing variable here and there base on tutorial, but can't seem to get it right.** 

    SampleData * sample = (SampelData *) [self.myArr objectAtIndex:indexPath.row]; 
    cell.textLabel.text = @"%@ time %@ ",sample.clueName, sample.timeLog; 
    NSLog(@"Cell Value %d %@",indexPath.row, cell.textLabel.text); 
    return cell; 
} 

所以基本上你只需要改变你的方法定义两行。大多数情况下,您使用TableViews时都是这样的:将要读取数据的数组分配给自定义属性。返回tableView:numberOfRowsInSection:中的数组大小,并从适当的索引中获取对象以填充tableView:cellForRowAtIndexPath:中的单元格。

如果数组内容发生更改,则必须执行额外操作才能更新表视图。

1
  • 首先在.h文件中声明数组(例如:NSMutableArray *arrStopwatchDetails)。
  • 创建该数组的属性,如@property(nonatomic,retain)NSMutableArray *arrStopwatchDetails
  • 在.m文件中合成数组,如@synthesize arrStopwatchDetails
  • 在viewDidLoad中或在您想使用之前分配数组。

    ex。 self.arrStopwatchDetails = [[NSMutableArray alloc]init];

  • numberOfRowsInSection方法中,返回类似返回数组的计数[self.arrStopwatchDetails count]

  • In cellForRowsAtIndexPath方法,分配数组元素的值到单元的文本作为

的sampleData *样品= [[[SampleDataDAO的alloc] INIT] .self.arrStopwatchDetails objectAtIndex:indexPath.row]; cell.textLabel.text = @“%@ time%@”,sample.clueName,sample.timeLog; 就是这样。