سؤال

أود أن تتصرف My JitalView ليصبح مثل الجدول في محرر جهات الاتصال، أي يجب على المستخدم الضغط على تحرير ويجب أن يظهر صف "إضافة فئة جديدة" في أسفل كل قسم.

أنا أستخدم الرمز أدناه للقيام بذلك، ولكن المشكلة هي أنه لا يوجد انتقال سلس حيث يوجد اتصالات. بدلا من ذلك، يظهر الصف الجديد فجأة. كيف يمكنني الحصول على الرسوم المتحركة؟

أيضا، كيف يمكنني الرد على صف في صف "إضافة فئة جديدة"؟ الصف غير قابل للنقر في تطبيقي الحالي.

أحتاج إلى إعادة تحميل البيانات عند بدء تشغيل المستخدم التحرير؟ أنا أفعل هذا لأنه وإلا فإن صفوف الإدراج لا يتم استخلاصها أبدا.

شكرًا.

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];
    [tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)_tableView numberOfRowsInSection:(NSInteger)section {
    // ...
    if( self.tableView.editing ) 
        return 1 + rowCount;
}

- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // .....
    NSArray* items = ...;
    if( indexPath.row >= [items count] ) {
        cell.textLabel.text = @"add new category";
    }
    // ...

    return cell;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray* items = ...;

    if( indexPath.row == [items count] )
        return UITableViewCellEditingStyleInsert;

    return UITableViewCellEditingStyleDelete;
}
هل كانت مفيدة؟

المحلول

كنت في عداد المفقودين شيء واحد. في المعاشق:، بدلا من الاتصال ReaddData، كان ينبغي علي القيام به:

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated]; // not needed if super is a UITableViewController

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

    // fill paths of insertion rows here

    if( editing )
        [self.tableView insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationBottom];
    else
        [self.tableView deleteRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationBottom];

    [paths release];
}

نصائح أخرى

قد يتم الرد على النقرات على الصف في didSelectRowAtIndexPath الطريقة، ل indexPath.row == [items count]. وبعد للرسوم المتحركة، أقترح إلقاء نظرة هنا, ، في ال insertRowsAtIndexPaths:withRowAnimation: طريقة. هناك وظيفة حول كيفية استخدامه هنا.

التأثير الجانبي غير المرغوب فيه للحل المميز هو أن صف "إضافة" يتم إدراجه أيضا عندما يزداد المستخدم صف واحد واحد (شريطة تمكين التسوق). البرمجية التالية يحل هذه المعضلة:

// Assuming swipeMode is a BOOL property in the class extension.

- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Invoked only when swiping, not when pressing the Edit button.
    self.swipeMode = YES;
}

- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.swipeMode = NO;
}

سيتطلب الكود الخاص بك تغييرا صغيرا:

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated]; // not needed if super is a UITableViewController

    if (!self.swipeMode) {

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

        // fill paths of insertion rows here

        if( editing )
            [self.tableView insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationBottom];
        else
            [self.tableView deleteRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationBottom];
        [paths release];
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top