วิธีแก้ไขคุณสมบัติของ UITableViewCell ด้วยวิธีอื่น

ฉันรู้ว่ามันจะเป็นเซลล์ตั้งค่าด้วยวิธีนี้

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

เช่น ถ้าฉันต้องการแก้ไขคุณสมบัติของเซลล์ใน

tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

ฉันจะเข้าถึงเซลล์ที่ฉันเลือกได้อย่างไร?


person RAGOpoR    schedule 26.01.2010    source แหล่งที่มา


คำตอบ (1)


คุณสามารถรับเซลล์ที่มองเห็นได้ใน UITableView โดยใช้วิธีการต่อไปนี้:

-(UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath

ค่าที่ส่งคืน: วัตถุที่แสดงถึงเซลล์ของตารางหรือไม่มีเลยหากมองไม่เห็นเซลล์หรือ indexPath อยู่นอกช่วง

ดังนั้นในวิธี DidSelectRow ของคุณ คุณจะมีสิ่งที่คล้ายกัน (คุณอาจต้องตั้งค่า SelectStyle ของเซลล์เป็น UITableViewCellSelectionStyleNone เพื่อให้การเปลี่ยนแปลงของคุณแสดงอย่างถูกต้อง):

- tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell != nil){
        cell.textLabel.textColor = [UIColor redColor];
    }
}

หรือคุณสามารถคลาสย่อย UITableViewCell ใช้วิธี - (void)setSelected:(BOOL)selected animated:(BOOL)animated และเปลี่ยนคุณสมบัติของเซลล์ที่นั่น

person Vladimir    schedule 26.01.2010
comment
คุณช่วยแสดงตัวอย่างให้ฉันดูสำหรับคำตอบแรกได้ไหม ในคำตอบที่สอง ฉันคิดว่าคุณบอกให้ฉันใช้ UITableViewCell ที่กำหนดเองแล้วแทนที่ - (void)setSelected:(BOOL)selected Animated:(BOOL)animated to handle ใช่ไหม? - person RAGOpoR; 26.01.2010