1

背景:折りたたんだり展開したりできるセクション化されたテーブルビューを実装しようとしています。折りたたむと、テーブルは選択されたものを記憶し、展開されたときに選択された状態を保持します。

問題: a セクションが展開され、正常に機能します。セルが適切に選択されました。ただし、同じ場所の次のセクションのセルも強調表示されているように見えました。

例: ABの2 つのセクションがあります。各セクションには、 abcの3 つのセルがあります。すべてのセルが画面に表示され、何も選択されないように、両方のセクションを展開します。次に ( A , a )をタップします。セル ( A , a ) が強調表示され、すべて問題ありません。セクションAを折りたたんでから、セクションAを展開します。( A , a ) は強調表示されます。ただし、( B , a ) も同様に強調表示されます。これは ( A , bで繰り返すことができます) および ( A , c )。それぞれ ( B , b ) と ( B , c )が点灯します。

プロジェクト: <削除済み>

コード:

viewController.m これに詳細事項を保持します。

@interface ViewController ()

@end

@implementation ViewController{
    UITableView * _tableView;

    BOOL aOpen;
    BOOL bOpen;
    BOOL cOpen;

    NSIndexPath *selectedIndexPath;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.allowsMultipleSelection = NO;
    [self.view addSubview:_tableView];
}

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

#pragma mark - Table View stuff
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    switch (section) {
        case 0:
            return [[TableViewHeader alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 40) title:@"Section A" section:section isOpen:aOpen delegate:self];
            break;
        case 1:
            return [[TableViewHeader alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 40) title:@"Section B" section:section isOpen:bOpen delegate:self];
            break;
        case 2:
            return [[TableViewHeader alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 40) title:@"Section C" section:section isOpen:cOpen delegate:self];
            break;
        default:
            return Nil;
            break;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            return aOpen? 3:0;
            break;
        case 1:
            return bOpen? 4:0;
            break;
        case 2:
            return cOpen? 5:0;
            break;
        default:
            return 0;
            break;
    }

}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.contentView.backgroundColor = [UIColor grayColor];
        cell.contentView.alpha = .4;
    }

    if ([selectedIndexPath isEqual:indexPath]) {//if it was already selected
        [tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    }
    return cell;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 74;
}

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

}

//for high visibility purpose
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (section == 2) {
        UIView *view = [[UIView alloc] init];
        return view;
    }

    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 2) {
        return 1;
    }
    return 0;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    selectedIndexPath = indexPath;
    [tableView reloadData];
}

#pragma mark - header delegate handler
- (void) headerTappedAtSection: (int) section {
    BOOL openAction = false;
    int numberOfRow;
    switch (section) {
        case 0:
            aOpen = !aOpen;
            openAction = aOpen;
            numberOfRow = 3;
            break;
        case 1:
            bOpen = !bOpen;
            openAction = bOpen;
            numberOfRow = 4;
            break;
        case 2:
            cOpen = !cOpen;
            openAction = cOpen;
            numberOfRow = 5;
            break;
        default:
            break;
    }
    [_tableView reloadSections: [NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationAutomatic];
    if (openAction && numberOfRow) {
        [_tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section] atScrollPosition:UITableViewScrollPositionTop animated:YES];
    }
}
@end
4

3 に答える 3

2

次のことをお勧めします。

tableView から削除: cellForRowAtIndexPath

if ([selectedIndexPath isEqual:indexPath]) {//if it was already selected
     [tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}

そして、reloadSection の後にヘッダー デリゲート ハンドラー関数を追加します (おそらく if (openAction && numberOfRow){} 内)。

     [_tableView selectRowAtIndexPath:selectedIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];

これにより、セクションがリロードされた後にセルが選択されていることが確認されます。また、 selectedCellIndex が保存されているため、それを使用してセルを選択できるはずです。

于 2012-11-08T17:48:46.533 に答える
0

変えようとする

[_tableView reloadSections: [NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationAutomatic];

シンプルな..

 [_tableView reloadData];
于 2012-11-08T05:25:12.220 に答える
0

あなたのテーブルビューは にallowsMultipleSelection設定されていYESますか? NOその場合は、 を呼び出したときにテーブル ビューがセクションに追加するのではなく、セクションを置き換えるように設定する必要がありますselectRowAtIndexPath:animated:scrollPosition:

于 2012-11-07T16:32:32.763 に答える