开发者

How to copy TableView cell data to a NSMutable Array?

开发者 https://www.devze.com 2023-03-15 22:24 出处:网络
I\'m quite new to iphone development. I created To-Do List app using coredata. so, my table view dynamically updating.

I'm quite new to iphone development. I created To-Do List app using coredata. 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)

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开发者_StackOverflow社区 (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]]; some where out of the cellForRowAtIndexPath: method, so that it won't be reallocated every time a cell loads. And you can have the line [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.

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.

Best way would be to use your oneHero object at the specific index to find the text.


so simple , define a 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"];
0

精彩评论

暂无评论...
验证码 换一张
取 消