美文网首页
UITableView's reusableTableC

UITableView's reusableTableC

作者: Crazy2015 | 来源:发表于2017-05-11 09:32 被阅读56次

    @implementation UITableView (T)

    • (NSMutableArray *)reusableCells
      {
      return [self performSelector:@selector(_reusableTableCells)];
      }

    @end

    @interface ViewController () <UITableViewDataSource, UITableViewDelegate>

    @property (nonatomic, strong) UITableView *tableView;

    @end

    @implementation ViewController

    • (void)viewDidLoad
      {
      [super viewDidLoad];

      self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
      self.tableView.delegate = self;
      self.tableView.dataSource = self;

      [self.view addSubview:self.tableView];

      [self reloadData];

      [self performSelector:@selector(reloadData) withObject:nil afterDelay:.8];
      }

    • (void)reloadData
      {
      NSLog(@"--------------------");
      NSLog(@"before reloadData");
      NSLog(@"%@", [self.tableView valueForKey:@"_reusableTableCells"]);

      [self.tableView reloadData];

      NSLog(@"after reloadData");
      NSLog(@"%@", [self.tableView valueForKey:@"_reusableTableCells"]);
      }

    • (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
      {
      return 1;
      }
    • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
      {
      return 3;
      }

    • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      {
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"test"];
      if (!cell) {
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"test"];
      }
      NSLog(@"make cell row:%d", indexPath.row);

      return cell;
      }
      @end

    2014-10-17 19:23:09.334 test[52333:4372534] --------------------
    2014-10-17 19:23:09.335 test[52333:4372534] before reloadData
    2014-10-17 19:23:09.335 test[52333:4372534] (null)
    2014-10-17 19:23:09.335 test[52333:4372534] after reloadData
    2014-10-17 19:23:09.335 test[52333:4372534] {
    }
    2014-10-17 19:23:09.344 test[52333:4372534] make cell row:0
    2014-10-17 19:23:09.347 test[52333:4372534] make cell row:1
    2014-10-17 19:23:09.347 test[52333:4372534] make cell row:2
    2014-10-17 19:23:10.136 test[52333:4372534] --------------------
    2014-10-17 19:23:10.136 test[52333:4372534] before reloadData
    2014-10-17 19:23:10.137 test[52333:4372534] {
    }
    2014-10-17 19:23:10.137 test[52333:4372534] after reloadData
    2014-10-17 19:23:10.137 test[52333:4372534] {

        test =     (
            "<UITableViewCell: 0x7c3a4af0; frame = (0 88; 320 44); hidden = YES; autoresize = W; layer = <CALayer: 0x7c380f20>>",
            "<UITableViewCell: 0x7c3a4290; frame = (0 44; 320 44); hidden = YES; autoresize = W; layer = <CALayer: 0x7c3a4420>>",
            "<UITableViewCell: 0x7c39dfd0; frame = (0 0; 320 44); hidden = YES; autoresize = W; layer = <CALayer: 0x7c39e300>>"
        );
    
        其中test为NSMutableDictionary的key.
         (
            "<UITableViewCell: 0x7c3a4af0; frame = (0 88; 320 44); hidden = YES; autoresize = W; layer = <CALayer: 0x7c380f20>>",
            "<UITableViewCell: 0x7c3a4290; frame = (0 44; 320 44); hidden = YES; autoresize = W; layer = <CALayer: 0x7c3a4420>>",
            "<UITableViewCell: 0x7c39dfd0; frame = (0 0; 320 44); hidden = YES; autoresize = W; layer = <CALayer: 0x7c39e300>>"
        )
       为键对应的值,数据结构为数组。通过 对比数组中frame可以发现:
    数组第一个元素:frame = (0 88; 320 44);
    数组第二个元素:frame = (0 44; 320 44);
    数组第三个元素:frame = (0 0; 320 44);
    
    最先离开屏幕的元素,最近加入到数组中。同时,从数组中取元素的顺序也是,最先加入数组的元素,最先被取出来。这其实就是**队列**的思想。
    同时我们也可以从方法的命名看出来问题:
    dequeueReusableCellWithIdentifier:
    

    }
    2014-10-17 19:23:10.137 test[52333:4372534] make cell row:0
    2014-10-17 19:23:10.138 test[52333:4372534] make cell row:1
    2014-10-17 19:23:10.138 test[52333:4372534] make cell row:2

    参考:
    https://gist.github.com/hirohisa/36b61d628a3a1e6866fe
    https://github.com/Microsoft/WinObjC/blob/master/Frameworks/UIKit/UITableView.mm

    相关文章

      网友评论

          本文标题:UITableView's reusableTableC

          本文链接:https://www.haomeiwen.com/subject/iiontxtx.html