2016-01-13 80 views
0

FirstPage.h我的tableview的細胞沒有顯示

#import <UIKit/UIKit.h> 
#import "StudentPage.h" 

@interface FirstPage : UIViewController <UITableViewDelegate,UITableViewDataSource,StudentInfoDelegates> 
@property (strong, nonatomic) IBOutlet UITableView *tableView; 
- (IBAction)addButtonAction:(id)sender; 

@end 

FirstPage.m

#import "FirstPage.h" 

@interface FirstPage() 

@end 

@implementation FirstPage 
{ 
    NSMutableArray *firstPageArray; 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib 
    //[self performSegueWithIdentifier:@"StudentInfo" sender:sender]; 
    /*StudentPage *student=[[StudentPage alloc]init]; 
    student.delegates=self;*/ 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

//the next following didn't created.. 
//the RowAtIndexPath are not been executed.. 

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    NSString *[email protected]""; 

    NSMutableDictionary *studentSecondDictionary=[[NSMutableDictionary alloc]init]; 
    static NSString *[email protected]"Cell"; 
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if(cell==nil){ 
     cell=[[UITableViewCell alloc]initWithFrame:CGRectZero]; 
     [cell setBackgroundColor:[UIColor redColor]]; 
     [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
     [cell setIndentationWidth:0.0]; 

     studentSecondDictionary=[firstPageArray objectAtIndex:indexPath.row]; 
     NSString *name1=[studentSecondDictionary valueForKey:@"NAME"]; 
     NSString *regno1=[studentSecondDictionary valueForKey:@"REGNO"]; 
     NSString *marks1=[studentSecondDictionary valueForKey:@"MARKS"]; 
     NSString *rank1=[studentSecondDictionary valueForKey:@"RANK"]; 
     concateValue=[[[[[[name1 stringByAppendingString:@" "]stringByAppendingString:regno1]stringByAppendingString:@" "]stringByAppendingString:marks1]stringByAppendingString:@" "]stringByAppendingString:rank1]; 
     cell.textLabel.text=concateValue; 
    } 
    return cell; 
} 

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ 
    return 1; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return firstPageArray.count; 
} 

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    return 50; 
} 

- (IBAction)addButtonAction:(id)sender { 
    StudentPage *stdView=[self.storyboard instantiateViewControllerWithIdentifier:@"MyIdentifier"]; 
    stdView.delegates=self; 
    [self.navigationController pushViewController:stdView animated:YES]; 
    //[self.view addSubview:stdView.view]; 
} 

-(void)didFinishSave:(NSMutableArray *)in_studentList{ 
    firstPageArray=[[NSMutableArray alloc]init]; 
    firstPageArray=in_studentList; 
    [self.tableView reloadData]; 
} 

@end 

StudentPage.h

#import <UIKit/UIKit.h> 

@protocol StudentInfoDelegates 
-(void)didFinishSave:(NSMutableArray*)in_studentList; 
@end 

@interface StudentPage : UIViewController<UITextFieldDelegate> 
@property(assign,nonatomic)id<StudentInfoDelegates> delegates; 

@property (strong, nonatomic) IBOutlet UITextField *name; 
@property (strong, nonatomic) IBOutlet UITextField *regno; 
@property (strong, nonatomic) IBOutlet UITextField *marks;  
@property (strong, nonatomic) IBOutlet UITextField *rank; 

- (IBAction)save:(UIButton *)sender; 
@end 

StudentPage.m

#import "StudentPage.h" 
#import "FirstPage.h" 
@implementation StudentPage 
{ 
    NSMutableArray *studentArray; 
    NSMutableDictionary *StudentDictionary; 
} 
@synthesize name,regno,marks,rank; 
@synthesize delegates; 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
    [self.view endEditing:YES]; 
    [super touchesBegan:touches withEvent:event]; 
} 

- (IBAction)save:(UIButton *)sender { 
    studentArray=[[NSMutableArray alloc]init]; 
    StudentDictionary=[[NSMutableDictionary alloc]init]; 
    [StudentDictionary setValue:name.text forKey:@"NAME"]; 
    [StudentDictionary setValue:regno.text forKey:@"REGNO"]; 
    [StudentDictionary setValue:marks.text forKey:@"MARKS"]; 
    [StudentDictionary setValue:rank.text forKey:@"RANK"]; 
    [studentArray addObject:StudentDictionary]; 

    [delegates didFinishSave:studentArray]; 
    NSLog(@"%@",studentArray); 
    FirstPage *firstView=[self.storyboard instantiateViewControllerWithIdentifier:@"MyIdentifier1"]; 
    [self.navigationController pushViewController:firstView animated:YES]; 
} 
@end 
+0

請首先正確格式化您的代碼 – Natasha

+0

您是否爲表視圖連接了'delegate'和'datasource'? – Gandalf

+0

是的,我連接了我的表格視圖的代表和數據源 –

回答

0

由於@BiWoj在評論中說,你檢查

if (cell==nil)... 

,並依靠該檢查要經過第一次,所以你可以填寫你的資料。這是錯誤的,你代替dequeuecell然後當它不是nil你可以開始把你的數據在viewscell將是nil的唯一原因是當您使用不存在的標識符(即不應該發生)的dequeue

0

首先,當您點擊保存按鈕時,檢查您的代理方法是否被調用。

我想你忘記了之前

[delegates didFinishSave:studentArray]; 

設置委託自我應該

self.delegate = delegates 
[delegates didFinishSave:studentArray]; 

如果不從這樣的第一頁工作,請檢查你正在推動學生頁面

- (IBAction)addButtonAction:(id)sender { 
StudentPage *stdView=[self.storyboard instantiateViewControllerWithIdentifier:@"MyIdentifier"]; 
stdView.delegates=self; 

[self.navigationController pushViewController:stdView animated:YES];} 

然後再一次在學生頁面的保存動作方法中,您是pushi像這樣的第一頁。

FirstPage *firstView=[self.storyboard instantiateViewControllerWithIdentifier:@"MyIdentifier1"]; 
[self.navigationController pushViewController:firstView animated:YES]; 

嗯,我想不是的,你必須只使用

[self.navigationController popToRootViewControllerAnimated:YES]; 

沒有必要再次推你只需要流行到基本控制器相同的控制器。