Objective c 目标c-将对象数组传递到表视图

Objective c 目标c-将对象数组传递到表视图,objective-c,uitableview,Objective C,Uitableview,我需要传递类对象数组 我有一个名为Student的类,它有一些属性(名称、代码、帐户类型……) 我在数据库中调用方法以获取所有学生对象 Student_array = [data_base_helper selectAllStudents]; 我需要在表视图中显示学生姓名列表,我制作了另一个名为names的数组 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

我需要传递类对象数组 我有一个名为Student的类,它有一些属性(名称、代码、帐户类型……)

我在数据库中调用方法以获取所有学生对象

Student_array = [data_base_helper selectAllStudents];
我需要在表视图中显示学生姓名列表,我制作了另一个名为names的数组

- (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;
}

我发现delete存在一些问题,因为我按索引删除想法如何在表视图中传递对象数组

想法是您不需要使用“名称数组”,您可以直接使用Students数组,方法如下:

- (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\u数组是
NSMutableArray
而不是
NSArray


注:您可能希望在
Student
中重命名
Student\u数组
,因为在objective-c中,用大写字母命名对象和使用下划线都不是一个好的做法。可能我也会在
name
中重命名
student\u name
,因为student只是对象类型的重复。

当您从表中删除单元格时,请同时删除与已删除单元格对应的数组student\u数组和self.name数组的项