我在将数据从展开的segue中转换为NSMutableArray对象时遇到了一些麻烦。当我从unwind segue中执行NSLog时,它会显示信息存在,它不会在我的数组中最终显示在表视图中。我的问题是什么?谢谢大家。
#import "ChoicesViewController.h"
#import "MyDataChoices.h"
#import "AddChoiceViewController.h"
@interface ChoicesViewController () <UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property (strong, nonatomic) NSMutableArray *arrayNames;
@property (strong, nonatomic) NSArray *sections;
@end
@implementation ChoicesViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.myTableView.delegate = self;
self.myTableView.dataSource = self;
self.sections = [NSArray arrayWithObjects:@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V,", @"W", @"X", @"Y", @"Z", nil];
self.arrayNames = [[NSMutableArray alloc] init];
[self.arrayNames addObjectsFromArray:@[
[MyDataChoices itemWithNewName:@"Apples"],
[MyDataChoices itemWithNewName:@"Bread"]]];
}
- (IBAction)unwindSegueToChoices:(UIStoryboardSegue *)segue
{
AddChoiceViewController *sourceVC = segue.sourceViewController;
NSString *myNewItem = sourceVC.myTextField.text;
//NSString *myFinalString = [[myNewItem substringToIndex:1] capitalizedString];
NSString *capitalizedString = [myNewItem capitalizedString];
NSLog(capitalizedString);
//Why will this not work?
[self.arrayNames addObject:capitalizedString];
}
-(NSInteger) numberOfSectionsInTableView:(UITableView *) tableView
{
return 1;
}
-(NSInteger) tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger)section
{
return self.arrayNames.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyDataChoices *currentRow = self.arrayNames[indexPath.row];
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"mainCell2" forIndexPath:indexPath];
cell.textLabel.text = currentRow.myNameChoices;
return cell;
}
答案 0 :(得分:1)
在将新项添加到数组后,您需要重新加载表视图:
[self.tableView reloadData];