Reference from UITableViewCell to parent UITableView?

前端 未结 7 1732
有刺的猬
有刺的猬 2020-11-29 04:02

Is there any way to access the owning UITableView from within a UITableViewCell?

7条回答
  •  鱼传尺愫
    2020-11-29 04:41

    If you have custom classes for your UITableViewCells, you can add an id type variable in your cell's header, and synthesize the variable. After you set the variable when you load the cell, you are free to do what you please with the tableview or any other higher view without much hassle or overhead.

    cell.h

     // interface
     id root;
    
     // propery 
     @property (nonatomic, retain) id root;
    

    cell.m

    @synthesize root;

    tableviewcontroller.m

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      // blah blah, traditional cell declaration
      // but before return cell;
      cell.root = tableView;
    }
    

    Now you can call any of the tableview's methods from within your cell using the root variable. (e.g., [root reloadData]);

    Ah, takes me back to the good old days of flash programming.

提交回复
热议问题