我的问题是我无法更新我的表,我用NSMutableDictionary填充了表,我设法添加一个带有plusSign的UITabBarButton,用一个带有textFields的新视图。新对象添加到Dictionary /数组我可以看到[arrayKA count]已经变为+1,但tableView永远不会更新。我一直在尝试使用[self.tableView relodData],但我得到一个警告“属性TableView未在类型为FourthViewController的对象上找到”
TableView中设置的类是我发布的类。 一切都很好用,直到我开始将项目添加到我的表
如果能得到一些帮助,我会很高兴。
哦......还有一件事,我在“addView”中使用与类相同的类我觉得它很丑,但这是最简单的方法,对吧? :)
//
// FourthViewController.m
#import "FourthViewController.h"
#import "DetailViewController.h"
@implementation FourthViewController
@synthesize textField_newKA_name = _textField_newKA_name;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[self setupArray];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addTapped:)];
[super viewDidLoad];
}
/*
-(void)viewWillAppear:(BOOL)animated{
[self.tableView reloadData]; <--cannot write this =(
}
*/
- (void)addTapped:(id)sender
{
// push the new view - The AddNewKaView
FourthViewController *pushNewKA = [self.storyboard instantiateViewControllerWithIdentifier:@"newKA"];
[self.navigationController pushViewController:pushNewKA animated:YES];
pushNewKA.title = @"adding a new KA";
}
- (IBAction)saveNewKA:(id)sender {
NSMutableDictionary * newKA = [[NSMutableDictionary alloc] init ];
[newKA setObject:self.textField_newKA_name forKey:@"name"];
[arrayKA addObject:newKA]; // adding the newKA to the arrayKA
}
- (void)setupArray {
arrayKA = [[NSMutableArray alloc] init];
NSMutableDictionary *kaliber1 = [[NSMutableDictionary alloc] init];
[kaliber1 setObject:@"AK1" forKey:@"name"];
[kaliber1 setObject:@"ak1.jpg" forKey:@"image"];
[arrayKA addObject:kaliber1];
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [arrayKA count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//population the Table from arrayKA, right?
cell.textLabel.text = [[arrayKA objectAtIndex:indexPath.row] objectForKey:@"name"];
if ([UIImage imageNamed:[[arrayKA objectAtIndex:indexPath.row] objectForKey:@"image"]] != nil) {
cell.imageView.image = [UIImage imageNamed:[[arrayKA objectAtIndex:indexPath.row] objectForKey:@"image"]];
}
else //Missing picture
{
cell.imageView.image = [UIImage imageNamed:@"ra3Rocket.jpg"];
}
return cell;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete){
[arrayKA removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *__strong)indexPath {
DetailViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"modulKA"];
[self.navigationController pushViewController:detail animated:YES];
detail.titleField.text = [[arrayKA objectAtIndex:indexPath.row] objectForKey:@"name"];
detail.title = [[arrayKA objectAtIndex:indexPath.row] objectForKey:@"name"];
detail.textField_KA_initialhast.text = [[arrayKA objectAtIndex:indexPath.row] objectForKey:@"InitialHastKula"];
NSString *ValtVapensKulDiameter = [[arrayKA objectAtIndex:indexPath.row] objectForKey:@"kulDiameter"];
NSString *ValtVapensKulaTalB = [[arrayKA objectAtIndex:indexPath.row] objectForKey:@"TalB"];
NSString *ValtVapensKulInitialHast = [[arrayKA objectAtIndex:indexPath.row] objectForKey:@"InitialHastKula"];
ValtVapensKulDiameterNUM = ([ValtVapensKulDiameter floatValue] );
ValtVapensKulaTalBNUM = ([ValtVapensKulaTalB floatValue] );
ValtVapensKulInitialHastNUM = ([ValtVapensKulInitialHast floatValue] );
[[arrayKA objectAtIndex:indexPath.row] objectForKey:@"kulDiameter"];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end
我在互联网上找到了这些线路的想法:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow: arrayKA.count-1 inSection:0];
NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
[self.tableView insertRowAtIndexPaths:indexPaths withRowAnimation:YES];
也许我会用它来为我的newKA在数组中“腾出空间”,但我仍然无法写[self.tableView....