好的,所以我可能真的很喜欢编程,但是我已经有一段时间没有解决这个问题了,也无法解决这个问题。我有可扩展 tableView
动态"添加" 和"删除" 行
两个问题:
1)我可以在单元格中加载textFields
和labels
的唯一方法是展开tableView
并单击加载按钮。我保存textFields
和labels
的代码位于expandingcell.m
而不是viewController.m
。我希望我的信息在启动时加载。
2)如果我保存单元格信息,它会保存我在每个单元格中的信息,所以当我加载它时,每单个单元格将加载相同的东西,当我单击加载按钮。 任何帮助都会非常感激我在这个上花了无数个小时
#import "ExpandingCell.h"
@implementation ExpandingCell
@synthesize DateLabel, CourseLabel, Date, Course, Score, Par, Putts, GIR;
- (IBAction)Save:(id)sender
{
NSString *saveString = Date.text;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:saveString forKey:@"saveString"];
[defaults synchronize];
NSString *saveString1 = Course.text;
NSUserDefaults *defaults1 = [NSUserDefaults standardUserDefaults];
[defaults1 setObject:saveString1 forKey:@"saveString1"];
[defaults1 synchronize];
NSString *saveString2 = Score.text;
NSUserDefaults *defaults2 = [NSUserDefaults standardUserDefaults];
[defaults2 setObject:saveString2 forKey:@"saveString2"];
[defaults2 synchronize];
NSString *saveString3 = Par.text;
NSUserDefaults *defaults3 = [NSUserDefaults standardUserDefaults];
[defaults3 setObject:saveString3 forKey:@"saveString3"];
[defaults3 synchronize];
NSString *saveString4 = Putts.text;
NSUserDefaults *defaults4 = [NSUserDefaults standardUserDefaults];
[defaults4 setObject:saveString4 forKey:@"saveString4"];
[defaults4 synchronize];
NSString *saveString5 = GIR.text;
NSUserDefaults *defaults5 = [NSUserDefaults standardUserDefaults];
[defaults5 setObject:saveString5 forKey:@"saveString5"];
[defaults5 synchronize];
}
- (IBAction)Load:(id)sender
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *loadString = [defaults objectForKey:@"saveString"];
[Date setText:loadString];
[DateLabel setText:loadString];
NSUserDefaults *defaults1 = [NSUserDefaults standardUserDefaults];
NSString *loadString1 = [defaults1 objectForKey:@"saveString1"];
[Course setText:loadString1];
[CourseLabel setText:loadString1];
NSUserDefaults *defaults2 = [NSUserDefaults standardUserDefaults];
NSString *loadString2 = [defaults2 objectForKey:@"saveString2"];
[Score setText:loadString2];
NSUserDefaults *defaults3 = [NSUserDefaults standardUserDefaults];
NSString *loadString3 = [defaults3 objectForKey:@"saveString3"];
[Par setText:loadString3];
NSUserDefaults *defaults4 = [NSUserDefaults standardUserDefaults];
NSString *loadString4 = [defaults4 objectForKey:@"saveString4"];
[Putts setText:loadString4];
NSUserDefaults *defaults5 = [NSUserDefaults standardUserDefaults];
NSString *loadString5 = [defaults5 objectForKey:@"saveString5"];
[GIR setText:loadString5];
}
下一部分可能不那么重要我不知道,但我想我会粘贴所有内容,以便你了解我的程序做什么
#import "ViewController.h"
#import "ExpandingCell.h"
#import <QuartzCore/QuartzCore.h>
@interface ViewController ()
@property (strong, retain) IBOutlet UITableView *tableView;
- (IBAction)ADD:(id)sender;
@end
@implementation ViewController
-(void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[self tableView] reloadData];
self.navigationItem.leftBarButtonItem = self.editButtonItem;
self.tableView.delegate = self;
self.tableView.dataSource = self;
[_Fade setAlpha:0];
_tableView.layer.cornerRadius = 6.0f;
_tableView.backgroundColor=[[UIColor alloc] initWithRed:0.0 green:0.0 blue:0.0 alpha:0.7];
_tableView.separatorColor = [UIColor blueColor];
//set index to -1 to say no cell is expanded or should expand
selectedIndex = -1;
titleArray = [[NSMutableArray alloc] init];
//adds first row for the user
NSUserDefaults *theDefault = [NSUserDefaults standardUserDefaults];
titleArray = [theDefault objectForKey:@"titleArray"] ;
[theDefault synchronize];
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return titleArray.count;
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"expandingCell";
ExpandingCell *cell = (ExpandingCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ExpandingCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.autoresizingMask = UIViewAutoresizingFlexibleHeight;
cell.clipsToBounds = YES;
}
return cell;
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (selectedIndex == indexPath.row)
{
return 180;
}
else
{
return 40;
}
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (selectedIndex == indexPath.row)
{
selectedIndex = -1;
[UIView transitionWithView:tableView
duration:0.5f
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^(void) {
[tableView reloadData];
} completion:NULL];
return;
}
//just in case selected index changes for whatever reason
if (selectedIndex != -1)
{
NSIndexPath *prevPath = [NSIndexPath indexPathForRow:selectedIndex inSection:0];
selectedIndex = indexPath.row;
[tableView reloadRowsAtIndexPaths: [NSArray arrayWithObjects:prevPath, nil] withRowAnimation:UITableViewRowAnimationFade];
}
selectedIndex = indexPath.row;
[UIView transitionWithView:tableView
duration:0.5f
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^(void) {
[tableView reloadData];
} completion:NULL];
}
- (BOOL) tableView: (UITableView *) tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)ADD:(id)sender
{
[titleArray addObject:@"ExpandingCell"];
[[self tableView] reloadData];
}
- (void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[titleArray removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
}
}
- (void) setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[self.tableView setEditing:editing animated:animated];
}
- (IBAction)SaveTable:(id)sender
{
NSUserDefaults *theDefault = [NSUserDefaults standardUserDefaults];
[theDefault setObject:titleArray forKey:@"titleArray"];
[theDefault synchronize];
[_Fade setAlpha:100];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[_Fade setAlpha:0];
[UIView commitAnimations];
}