【发布时间】:2014-07-15 02:26:53
【问题描述】:
数据值我有一个文本文件和两个按钮。一个按钮是保存,第二个按钮是显示。 当单击保存按钮数据将存储在核心数据中,然后我单击显示按钮数据将显示在控制台上。一切顺利。 但现在我想当点击显示按钮数据将显示在 UITableView 所以请给我任何想法 提前致谢, 这是我的代码:-
#import "SetViewController.h"
#import "AppDelegate.h"
@interface SetViewController ()
{
NSMutableArray *array;
}
@end
@implementation SetViewController
@synthesize textField;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)save:(id)sender {
AppDelegate *appD=(AppDelegate *) [[UIApplication sharedApplication]delegate];
NSEntityDescription *entit=[NSEntityDescription insertNewObjectForEntityForName:@"Team" inManagedObjectContext:appD.managedObjectContext];
[entit setValue:textField.text forKey:@"fname"];
NSError *error;
BOOL isSaved=[appD.managedObjectContext save:&error];
NSLog(@"succesfully saved flag: %d",isSaved);
}
- (IBAction)show:(id)sender {
AppDelegate *appDelegat=(AppDelegate*)[[UIApplication sharedApplication]delegate];
NSEntityDescription *entity=[NSEntityDescription entityForName:@"Team" inManagedObjectContext:appDelegat.managedObjectContext];
NSFetchRequest *fetchRr = [[NSFetchRequest alloc]init];
[fetchRr setEntity:entity];
array=[[appDelegat.managedObjectContext executeFetchRequest:fetchRr error:nil]mutableCopy];
NSLog(@"array %@",array);
for (NSManagedObject *obj in array) {
NSLog(@"Name :%@\n",[obj valueForKey:@"fname"]);
}
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
@end
i am tried like this :-
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"ShowData"])
{
ShowViewController *destViewController = segue.destinationViewController;
destViewController.second = [[array valueForKey:@"fname"] componentsJoinedByString(angry)" , "];
NSLog(@" got data for array %@",destViewController.second);
}
}
ShowViewController.h:-
#import <UIKit/UIKit.h>
#import "SetViewController.h"
@interface ShowViewController : UITableViewController<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) NSString *second;
@end
ShowViewController.m:-
import "ShowViewController.h"
@interface ShowViewController ()
{
NSMutableArray *acess;
}
@end
@implementation ShowViewController
@synthesize second;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
acess =[second componentsSeparatedByString:@", "];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [acess count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
}
cell.textLabel.text = [acess objectAtIndex:indexPath.row];
return cell;
}
ShowViewController 是 UITableView。当点击 UItableView 时打开但数据不会显示所以请给我任何想法
【问题讨论】:
-
句间加点更容易理解
-
您是否研究过 tableview 编程指南或搜索过 tableview 教程 - 周围有很多。您需要实现表视图委托和数据源协议
-
@Paulw11 感谢重播现在我添加代码请检查然后给我任何关于我的问题的想法
-
第二个属性是什么类型? ShowViewController 中的 cellForRowAtIndexPath 方法是什么?
-
@Paulw11 请看一下我的代码现在添加 ShowViewController 然后给我任何想法
标签: ios objective-c uitableview core-data