سؤال

لدي عرض جدول يحتوي على قائمة بأسماء المستخدمين التي يتم فهرستها في الأقسام والصفوف أبجديًا. عندما أقوم بالنقر فوق أحد الصفوف في قسم ما ، تتم إضافة المستخدم الصحيح إلى صفيف المستفيدين ويكون علامة الاختيار أماكن في الخلية إلى جانب اسمها .. ولكن يتم عرض علامة فحص بجوار أسماء المستخدمين الأخرى التي لم يتم تحديدها و ليست في صفيف المستفيدين. لقد حاولت إعادة تعيين الخلية المحددة باستخدام IndexPath جديد (انظر الكود أدناه) ولكن لم أتمكن من العمل. يسجل المسار الصحيح ، لكنه لن يعينه. أنا أستخدم طريقة مماثلة لتعيين المستخدمين الصفوف في كل قسم دون أي مشكلة ولكن لسبب ما تمنحني علامات الملحقات مشاكل. لقد رأيت بعض المواضيع الأخرى على الفائض حول هذا الموضوع نفسه ولكن اغسل ؛ "قادر على الوصول إلى حل لحالتي. أي أدلة؟ في صحتك!

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

    int row = indexPath.row;
    int section = indexPath.section;
    NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:row inSection:section];

    [tableView deselectRowAtIndexPath:newIndexPath animated:NO];

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:newIndexPath];

    NSArray *array = [self.sectionsArray objectAtIndex:indexPath.section];
    PFUser *user = [array objectAtIndex:indexPath.row];

    if (cell.accessoryType == UITableViewCellAccessoryNone) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        [self.recipients addObject:user];
    }
    else {
        cell.accessoryType = UITableViewCellAccessoryNone;
        [self.recipients removeObject:user];
    }

    [self.currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (error) {
            NSLog(@"Error %@ %@", error, [error userInfo]);
        }
    }];

وإليك cellforrowatindexpath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *CellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Get the user names from the array associated with the section index in the sections array.
    NSArray *userNamesInSection = (self.sectionsArray)[indexPath.section];

    // Configure the cell with user name.
    UserNameWrapper *userName = userNamesInSection[indexPath.row];
    cell.textLabel.text = userName.user;

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

المحلول

كما أرى أنك ارتكبت أخطئين في CellForrowatIndexPath التي لم تتحقق مما إذا كانت الخلية لاغية لإنشاء واحدة وتعيين الملحقات للخلية وفقًا لقائمة المستلم.

يجب أن تفعل مثل:

NSString *CellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

PFUser *user = [self getUserAtIndexPath:indexPath];
cell.textLabel.text = user.name;

if ([self.recipients containObject:user]) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
    cell.accessoryType = UITableViewCellAccessoryNone;
}

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