简体   繁体   English

如何将 TableView 单元格数据复制到 NSMutable 数组?

[英]How to copy TableView cell data to a NSMutable Array?

I'm quite new to iphone development.我对iphone开发很陌生。 I created To-Do List app using coredata.我使用 coredata 创建了 To-Do List 应用程序。 so, my table view dynamically updating.所以,我的表格视图动态更新。 I want to add table view row textlabels to a NSMutable array (that means row1 text label to 1st index position of MutableArray, row2 text label to 2nd index position of Array) I want to add table view row textlabels to a NSMutable array (that means row1 text label to 1st index position of MutableArray, row2 text label to 2nd index position of Array)

this is my table view cellfor indexpath method这是我的表格视图单元格索引路径方法

- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *HeroTableViewCell = @"HeroTableViewCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:HeroTableViewCell];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:HeroTableViewCell] autorelease];
    }
    NSManagedObject *oneHero = [self.fetchedResultsController objectAtIndexPath:indexPath];
    NSInteger tab = [tabBar.items indexOfObject:tabBar.selectedItem];
    switch (tab) {
        case kByName:
            cell.textLabel.text = [oneHero valueForKey:@"name"];
            cell.detailTextLabel.text = [oneHero valueForKey:@"secretIdentity"];
            break;
        case kBySecretIdentity:
            cell.detailTextLabel.text = [oneHero valueForKey:@"name"];
            cell.textLabel.text = [oneHero valueForKey:@"secretIdentity"];
        default:
            break;
    }


    //listData = [[[NSMutableArray alloc] init]autorelease];

    //if(indexPath.row==1){
             //[listData addObject: cell.textLabel.text];


    return cell;
}

commented lines are how I'm try to do it.注释行是我尝试这样做的方式。 But I'm struggle to add those data as I want.但我很难根据需要添加这些数据。 Please help me请帮我

Have the line listData = [[[NSMutableArray alloc] init]];有行listData = [[[NSMutableArray alloc] init]]; some where out of the cellForRowAtIndexPath: method, so that it won't be reallocated every time a cell loads.一些在cellForRowAtIndexPath:方法之外的地方,这样就不会在每次加载单元格时重新分配它。 And you can have the line [listData addObject: [oneHero valueForKey:@"secretIdentity"]];你可以有这行[listData addObject: [oneHero valueForKey:@"secretIdentity"]]; where it is now currently.现在在哪里。

Edit: Better you can do this at some method, like viewDidLoad: , to make sure all elements are added to the array.编辑:更好的是,您可以在某些方法上执行此操作,例如viewDidLoad: ,以确保将所有元素添加到数组中。

listData = [[[NSMutableArray alloc] init]];

for (NSManagedObject *oneHero in [self.fetchedResultsController allObjects]) {

    [listData addObject: [oneHero valueForKey:@"secretIdentity"]];
}

First: you should retrieve this data from the datasource.首先:您应该从数据源中检索此数据。 Second: if you want to implement it the way you are doing it...then make sure you allocate the array only once like this:第二:如果你想以你正在做的方式实现它......然后确保你只分配一次数组,如下所示:

if(array == nil)
{
  array = [[NSMutableArray alloc] init];
}

This will have significant problems because cellForRow is called several times and with every reload you will end up adding duplicates.这将产生重大问题,因为 cellForRow 被多次调用,并且每次重新加载都会添加重复项。

Best way would be to use your oneHero object at the specific index to find the text.最好的方法是在特定索引处使用您的 oneHero object 来查找文本。

so simple, define a NSMUtableArray *arr = [NSMutableArray alloc] init];这么简单,定义一个 NSMUtableArray *arr = [NSMutableArray alloc] init];

NSMutableArray *arr = [[NSMutableArray alloc] init];

then add in the switch in each case然后在每种情况下添加开关

[arr addObject:@"Object You Want To Add"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM