簡體   English   中英

如何將 TableView 單元格數據復制到 NSMutable 數組?

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

我對iphone開發很陌生。 我使用 coredata 創建了 To-Do List 應用程序。 所以,我的表格視圖動態更新。 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)

這是我的表格視圖單元格索引路徑方法

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

注釋行是我嘗試這樣做的方式。 但我很難根據需要添加這些數據。 請幫我

有行listData = [[[NSMutableArray alloc] init]]; 一些在cellForRowAtIndexPath:方法之外的地方,這樣就不會在每次加載單元格時重新分配它。 你可以有這行[listData addObject: [oneHero valueForKey:@"secretIdentity"]]; 現在在哪里。

編輯:更好的是,您可以在某些方法上執行此操作,例如viewDidLoad: ,以確保將所有元素添加到數組中。

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

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

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

首先:您應該從數據源中檢索此數據。 第二:如果你想以你正在做的方式實現它......然后確保你只分配一次數組,如下所示:

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

這將產生重大問題,因為 cellForRow 被多次調用,並且每次重新加載都會添加重復項。

最好的方法是在特定索引處使用您的 oneHero object 來查找文本。

這么簡單,定義一個 NSMUtableArray *arr = [NSMutableArray alloc] init];

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

然后在每種情況下添加開關

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM