2014-09-21 48 views
0

我需要通过类的对象数组 我已经类称为学生,它有一些属性(名称,代码,ACCOUNT_TYPE ....);目标C - 传递对象的数组表视图

我调用数据库的方法来获取所有招生对象

Student_array = [data_base_helper selectAllStudents]; 

我需要显示在表视图学生名单,我制作的其它阵列叫名字

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [Student_array count]; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString * cell_identifer = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_identifer forIndexPath:indexPath]; 

    // Configure the cell... 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cell_identifer]; 
    } 



for(Student * st in student_array){ 
     [self.names addObject:st.student_name]; 
    } 

    cell.textLabel.text = [names objectAtIndex:indexPath.row ]; 

    return cell; 
} 

我发现一些问题与删除,因为我通过指数的想法删除如何传递对象数组中的表视图

回答

2

的想法是,你并不需要使用“名称的数组”,你可以用你的学生直接rray,以这样的方式

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [Student_array count]; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString * cell_identifer = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_identifer forIndexPath:indexPath]; 
    // Configure the cell... 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cell_identifer]; 
    } 

    Student *student = Student_array[indexPath.row]; 
    cell.textLabel.text = student.student_name; 

    return cell; 
} 

当你需要删除从一个的tableView学生,你可以很容易地从数组中删除,然后相应地更新的tableView,类似的东西:

[Student_array removeObjectAtIndex:indexPath.row]; 
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

注意,在上面的代码我假设你想在给定indexPath.row和Student_array删除的对象是NSMutableArray而不是NSArray

PS:您可能需要重命名Student_array in students因为在objective-c中,用大写字母命名对象并不是一个好习惯,也不是使用下划线。可能我也会重命名student_namename,因为学生只是对象类型的重复。

0

当您删除从表中的单元格中删除相应的删除单元两个阵列Student_array和self.name数组项。