我的tableview的单元格未显示

时间:2016-01-13 12:34:00

标签: ios objective-c uitableview

FirstPage.h

#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 *concateValue=@""; 

    NSMutableDictionary *studentSecondDictionary=[[NSMutableDictionary alloc]init];
    static NSString *CellIdentifier=@"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

2 个答案:

答案 0 :(得分:0)

正如@BiWoj在评论中所说,你正在检查

if (cell==nil)...

并依靠该检查第一次完成,以便您填写数据。这是错误的,您改为dequeue cell,然后当它不是nil时,您可以开始将数据放入viewscellnil的唯一原因是dequeue,使用不存在的标识符,即它永远不会发生。

答案 1 :(得分: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];}

再次在学生页面的保存操作方法中,您正在按这样推送第一页。

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

嗯,我想,而不是你必须使用

[self.navigationController popToRootViewControllerAnimated:YES];

无需再次按下弹出到基本控制器的同一个控制器。