سؤال

لدي مقتطف رمز يشبه هذا:

[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[array indexOfObject:[array objectAtIndex:indexPath.row]]] withRowAnimation:UITableViewRowAnimationLeft];
[tableView endUpdates];
[tableView reloadData];

يتم تنفيذه عند النقر على المستخدم على ملحق. الجزء الأول موجود فقط لتوفير الرسوم المتحركة السلسة ، ولا يهم حقًا ، حيث يتم إعادة تحميل TableView في وقت لاحق ، ولكن كما قلت ، هناك لتوفير الرسوم المتحركة.

من المفترض أن تنقل كائن محدد من فهرس الحالي إلى القيمة من صفيف في نفس الفهرس.

من الواضح أن هذا الرمز لا يعمل ، لذلك أريد فقط أن أعرف ما الذي يمكن فعله لإصلاحه؟

ملاحظة: أحصل على تحذير عند التجميع أيضًا. المعتاد "الحجة المارة 1 من" ArrayWithObject: "يجعل المؤشر من عدد صحيح بدون طاقم ..." (السطر 3)

انتهى الأمر بهذا المقتطف:

[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:[array indexOfObject:[arraySubFarts objectAtIndex:indexPath.row]] inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];

[tableView reloadData];
هل كانت مفيدة؟

المحلول

يمكنك استخدام ال NSIndexPath طريقة تمديد الفصل +indexPathForRow:inSection: لتحويل صف إلى مسار الفهرس. المزيد من التفاصيل هنا.

إذا كانت نيتك الوحيدة في حذف الصف وإدخالها هي التسبب في الرسوم المتحركة ، هل فكرت في reloadRowsAtIndexPaths:withRowAnimation: طريقة؟

نصائح أخرى

[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[array indexOfObject:[array objectAtIndex:indexPath.row]]] withRowAnimation:UITableViewRowAnimationLeft];

أخذ هذا الخط وتقسيمه ستحصل على ما يلي:

NSObject *obj = [array objectAtIndex:indexPath.row];
int *index = [array indexOfObject:obj];
NSArray *otherArray = [NSArray arrayWithObject:index];
[tableView insertRowsAtIndexPaths:otherArray withRowAnimation:UITableViewRowAnimationLeft];

ما تريده هو:

NSObject *obj = [array objectAtIndex:indexPath.row];
NSIndexPath *index = [NSIndexPath indexPathForRow:[array indexOfObject:obj] inSection:0];
NSArray *otherArray = [NSArray arrayWithObject:index];
[tableView insertRowsAtIndexPaths:otherArray withRowAnimation:UITableViewRowAnimationLeft];

ولكن لماذا لا يمكنك القيام بذلك؟

NSArray *otherArray = [NSArray arrayWithObject:indexPath];
[tableView insertRowsAtIndexPaths:otherArray withRowAnimation:UITableViewRowAnimationLeft];

يبدو الحصول على الكائن من الصفيف باستخدام فهرس ثم استخدام الكائن للعثور على الفهرس زائدة عن الحاجة. فقط استخدم الفهرس.


تحرير مع المزيد من الكود:

NSNumber *obj = [NSNumber numberWithInt:indexPath.row];
int *index = [array indexOfObject:obj];
NSIndexPath *index = [NSIndexPath indexPathForRow:index inSection:0];
NSArray *otherArray = [NSArray arrayWithObject:index];
[tableView insertRowsAtIndexPaths:otherArray withRowAnimation:UITableViewRowAnimationLeft];
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top