我想通过将字符串从UIViewController传递到UITableView来填充UITableView。
到目前为止,这是我的代码:
UITableViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
NSMArray = [[NSMutableArray alloc]initWithObjects:@"text",@"text2", nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return NSMArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier ];
}
cell.textLabel.text = [NSMArray objectAtIndex:indexPath.row];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
//remove fron NSMutableArray
[NSMArray removeObjectAtIndex:indexPath.row];
//delete from table
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
}
}
-(void)getString:(NSString *)string
{
if ([make length] > 0) {
if (!NSMArray)
{
NSMArray = [[NSMutableArray alloc]init];
}
[NSMArray insertObject:make atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.myTableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
UITableViewController.h
#import <UIKit/UIKit.h>
#import "ViewController.h"
@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UITableView *myTableView;
@property (strong, nonatomic) NSMutableArray *NSMArray;
-(void)getString:(NSString *)string;
ViewController.m
#import "entryViewController.h"
@interface entryViewController ()
@end
@implementation entryViewController
@synthesize textfieldString, myString;
- (void)viewDidLoad
{
[super viewDidLoad];
//Save button
UIBarButtonItem *SaveButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(pushCarMake:)];
self.navigationItem.rightBarButtonItem = SaveButton;
}
- (void)pushCarMake:(id)sender
{
myString = textfieldString.text;
ViewController *mainVC = [[ViewController alloc]init];
[mainVC getString:myString];
//pop to root ViewController
[self.navigationController popToRootViewControllerAnimated:TRUE];
}
@end
ViewCotroller.h
#import <UIKit/UIKit.h>
#import "ViewController.h"
@interface entryViewController : UIViewController
@property (strong, nonatomic) NSString *carMake;
@property (strong, nonatomic) IBOutlet UITextField *textfieldCarMake;
所以这很好用,并使用UITableViewCotroller的viewDidLoad中实例化的字符串项填充表视图。但每次我从ViewControllers传递一个字符串到表View时它不会进入表。任何建议为什么会这样。很抱歉粘贴了这么多代码。
答案 0 :(得分:1)
将数据从详细视图控制器传递回其父级的一种方法是使用委托。这是一个例子:
How do I set up a simple delegate to communicate between two view controllers?
<强> [编辑] 强>
如果您使用故事板,另一种方法是使用展开segue。以下是我使用此方法整理的示例:github.com/soleares/AddToTableView
答案 1 :(得分:0)
如果这是唯一的问题,则不是肯定的,但请尝试添加beginUpdates
和endUpdate
[myTableView beginUpdates];
[self.myTableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[myTableView endUpdates];