iPhone UITableViewController ย้ายเฉพาะแถวที่ระบุในตาราง

ฉันได้ใช้มาตรฐาน UITableViewController แล้ว ฉันได้สร้างเพียงบางแถวในตารางที่สามารถเคลื่อนย้ายได้เช่น canMoveRowAtIndexPath คืนค่าจริงสำหรับพาธดัชนีบางส่วนเท่านั้น (สำหรับแถวในส่วนแรกของตาราง) ฉันต้องการอนุญาตให้แลกเปลี่ยนเฉพาะแถวที่สามารถเคลื่อนย้ายได้เช่น โดยที่ canMoveRowAtIndexPath คืนค่าเป็นจริง

เช่น. ตารางของฉันมีลักษณะเช่นนี้:

Row 1
Row 2
Row 3
Row 4
Row 5

แถวที่ 1, 2, 3 สามารถเคลื่อนย้ายได้ ดังนั้นฉันต้องการใช้ลักษณะการทำงานต่อไปนี้: แถวที่ 1 สามารถแลกเปลี่ยนได้เฉพาะกับแถวที่ 2 หรือ 3 เท่านั้น ในทำนองเดียวกัน แถวที่ 2 สามารถแลกเปลี่ยนได้กับแถวที่ 1 และ 3 เท่านั้น นี่คือโครงร่างที่เป็นไปได้รูปแบบหนึ่ง:

Row 3
Row 1
Row 2
Row 4
Row 5

อย่างไรก็ตาม ฉันไม่อยากให้สิ่งนี้เกิดขึ้น:

Row 3
Row 5
Row 2
Row 4
Row 1

จะบรรลุเป้าหมายนี้ได้อย่างไร?


person Maggie    schedule 23.10.2010    source แหล่งที่มา


คำตอบ (2)


โปรดทราบว่าจริงๆ แล้วจะเป็นการย้ายเพียงแถวเดียว ไม่ใช่ การแลกเปลี่ยน

สิ่งที่คุณต้องการคือการดำเนินการ tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath

ดูวิธีจำกัดการเรียงลำดับแถว UITableView ใหม่ไปยังส่วนใดส่วนหนึ่ง

person Eiko    schedule 23.10.2010
comment
ขอบคุณมาก! targetIndexPathForMoveFromRowAtIndexPath แก้ไขปัญหาของฉัน - person Maggie; 23.10.2010

คุณสามารถควบคุมการเคลื่อนไหวภายในส่วนและระหว่างส่วนได้

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath

{

// Do not allow any movement between section
if ( sourceIndexPath.section != proposedDestinationIndexPath.section)
    return sourceIndexPath;
// You can even control the movement of specific row within a section. e.g last row in a     Section

// Check if we have selected the last row in section
if (   sourceIndexPath.row < sourceIndexPath.length) {
    return proposedDestinationIndexPath;
} else {
    return sourceIndexPath;
}

// คุณสามารถใช้วิธีการหรือตรรกะนี้เพื่อตรวจสอบดัชนีของแถวในส่วนและส่งคืน sourceIndexPath หรือ targetIndexPath

}

person Andy    schedule 27.07.2014