سؤال

لقد بحثت في جميع نتائج البحث تقريبًا، لكن خطأي لم يختفي.لدي عرض جدول تمت تهيئته بقسمين وصف واحد لكل منهما (من مصفوفة).لدي زر لعرض رأس القسم.عند النقر على زر أريد إضافة صف إلى القسم الأول.إليك الكود:

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [arr count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{   
    UITableViewCell *cell = (UITableViewCell*)[self.myTableView dequeueReusableCellWithIdentifier:@"DetailCell"];
    cell.textLabel.text=[arr objectAtIndex:indexPath.row];
    cell.backgroundColor=[UIColor clearColor];
    return cell; 
} 

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            btnReleases=[UIButton buttonWithType:UIButtonTypeCustom];
            [btnReleases setFrame:CGRectMake(10, 0, 30, 39)];
            [btnReleases setImage:[UIImage imageNamed:@"buttonr.png"] forState:UIControlStateNormal];
            [btnReleases addTarget:self action:@selector(loadReleases) forControlEvents:UIControlEventTouchUpInside];
            return btnReleases;
            break;
        case 1:
            btnLinks=[UIButton buttonWithType:UIButtonTypeCustom];
            [btnLinks setFrame:CGRectMake(10, 0, 30, 39)];
            [btnLinks setImage:[UIImage imageNamed:@"buttonl.png"] forState:UIControlStateNormal];
            [btnLinks addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
            return btnLinks;
            break;
        default:
            break;
    }

}
-(void)loadReleases
{

    [self.myTableView beginUpdates];
        [arr addObject:@"WTF"];
    NSArray *insert0 = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]]; NSLog(@"%@",insert0);
    [self.myTableView insertRowsAtIndexPaths:insert0 withRowAnimation:UITableViewRowAnimationBottom];
    [self.myTableView endUpdates];
}

وهنا الخطأ:

* فشل التأكيد في -[UITableView _endCellAnimationsWithContext:]، /SourceCache/UIKit_Sim/UIKit-1912.3/UITableView.m:1046

هل كانت مفيدة؟

المحلول

منذ كنت تستخدم [arr count] كقيمة الإرجاع ل tableView:numberOfRowsInSection:, ، عند نهاية ال beginUpdates…endUpdates يتوقع حظر tableView أنه سيكون هناك صفين في كل قسم من أقسام tableView الخاصة بك، ولكن مكالمتك لـ insertRowsAtIndexPaths: يشير فقط إلى أن الصف موجود في القسم 0.

تحتاج إلى إصلاح الخاص بك tableView:numberOfRowsInSection: لإرجاع قيم مختلفة حسب القسم:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return [arr count];
    } else {
        return 1;
    }
}

لاحظ أن هناك الكثير من الأشياء المريبة التي تحدث في عرض الطاولة الخاص بك:لديك قسمين، لكنك تعرض نفس الصف 0 بالضبط في كل قسم.وسلوك الإدراج داخل أقسامك متزعزع جدًا:تبدأ بإظهار صف واحد فقط، يتوافق مع الحالة 0 في ملفك switch, ، ثم تشير إلى أنك تقوم بإدراج صف في الصف 0، وبعد ذلك ستعرض الحالة 0 الصف في الصف 0 والصف 1 في الصف 1.لذا يجب عليك حقًا إدراج صف في الصف 1 بدلاً من الصف 0:

NSArray *insert0 = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:1 inSection:0]]; 
[self.myTableView insertRowsAtIndexPaths:insert0 withRowAnimation:UITableViewRowAnimationBottom];

نصائح أخرى

حاول مع رمز أدناه giveacodicetagpre.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top