0

我正在一个 iOS 原生应用程序开发团队实习暑假。他们给了我一项任务,即在不使用部分的情况下完成在 tableview 中删除/插入单元格。如果有人可以查看代码并尝试帮助我,我正在努力完成这项任务,我将不胜感激。我得到了一些删除和消失,但它并没有保持一致,它正在添加已经可见的单元格。(从某种意义上说,一旦我插入行,第 3 行现在是第 5 行,我的模运算就被抛弃了。

我对目标 c 非常陌生,因此非常感谢任何建议/帮助。

@implementation MasterViewController


NSMutableArray * levels;
NSMutableArray * array;
NSMutableIndexSet * expandedSections;
NSMutableDictionary * dict;

-(BOOL) loadFiles{
    //    NSFileManager * fileManager = [NSFileManager defaultManager];
    if (!expandedSections) {
        expandedSections = [[NSMutableIndexSet alloc]init];
    }

    array = [[NSMutableArray alloc]init];
    levels = [[NSMutableArray alloc]init];

    for (int i=0; i<20; i++) {
        NSString * temp = [[NSString alloc]initWithFormat:@"This is row : %i ", i];
        [array addObject:temp];
    }
    for (int i=0; i<20; i++) {
        NSNumber * temp = [[NSNumber alloc]initWithInt:i];
        [levels addObject:temp];
    }


    dict = [[NSMutableDictionary alloc]initWithObjects:array forKeys:levels];
    NSLog(@"TEST: %@", [dict objectForKey:[levels objectAtIndex:1]]);

    return YES;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"Master", @"Master");
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadFiles];

// Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.leftBarButtonItem = self.editButtonItem;

    UIBarButtonItem *addButton = [[UIBarButtonItem alloc]     initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self   action:@selector(insertNewObject:)];
    self.navigationItem.rightBarButtonItem = addButton;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)insertNewObject:(id)sender {
    if (!_objects) {
        _objects = [[NSMutableArray alloc] init];
    }
    [_objects insertObject:[NSDate date] atIndex:0];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView insertRowsAtIndexPaths:@[indexPath]  withRowAnimation:UITableViewRowAnimationAutomatic];
}

#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([expandedSections containsIndex:section]) {
        return [array count] +2;
    }
    return [array count];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    if (!(indexPath.row %3)==0) {
        cell.textLabel.text = [dict objectForKey:[levels objectAtIndex:indexPath.row]];
        [cell setIndentationLevel:2];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.accessoryView = nil; 
    } else {
        cell.textLabel.text = [dict objectForKey:[levels objectAtIndex:indexPath.row]];
        [cell setIndentationLevel:0];
    }

    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:  (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [_objects removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (!self.detailViewController) {
        self.detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
    }
    BOOL currentlyExpanded = [expandedSections containsIndex:indexPath.row];
    //[tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"CurrentlyExpanded : %d", currentlyExpanded);
    NSLog(@"TEST: %@", [dict objectForKey:[levels objectAtIndex:indexPath.row ]]);

    if ((indexPath.row %3)==0) {
        // only first row toggles exapand/collapse
        [tableView deselectRowAtIndexPath:indexPath animated:YES];

        NSInteger section = indexPath.section;
        BOOL currentlyExpanded = [expandedSections containsIndex:section];
        NSInteger rows;

        NSMutableArray *tmpArray = [NSMutableArray array];

        if (currentlyExpanded) {
            rows = 2;
            [expandedSections removeIndex:section];
        } else {
            [expandedSections addIndex:section];
            rows = 2;
        }

        for (int i=indexPath.row+1; i<=(indexPath.row + rows); i++) {
            NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i
                                                           inSection:section];
            [tmpArray addObject:tmpIndexPath];
        }

        if (currentlyExpanded) {
            [tableView deleteRowsAtIndexPaths:tmpArray
                             withRowAnimation:UITableViewRowAnimationTop];
        } else {
            [tableView insertRowsAtIndexPaths:tmpArray
                             withRowAnimation:UITableViewRowAnimationTop];
        }    
    }
    if ((indexPath.row %3)>0) {
        NSDate *object = _objects[indexPath.row];
        self.detailViewController.detailItem = object;
        [self.navigationController pushViewController:self.detailViewController animated:YES];
    }
}

@end
4

1 回答 1

0

Take a look at TLIndexPathTools. It greatly simplifies building dynamic tables like this. It does all the work calculating and performing the batch updates for you. Try running the Outline sample project. It demonstrates how to build an expandable tree without using sections. You can accomplish what you need by just building up a two-level tree.

于 2013-08-06T14:48:59.360 回答