2014年11月14日 星期五

tableView的cell的width會給定一個default值,reuse的時候才會以tableView為主

//如下NSLog所示,一開始進入cell == nil判斷時,cell的width都會是320,當你下拉tableView再拉回時,reuse的cell才會跟tableView的width一樣大。
//由此可知,一開始new的cell因為不知道你的tableView的frame,所以會先給定一個default值!你要自行去修改。

 static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
     
NSLog(@"%@ cellForRowAtIndexPath cell.frame.size.width 1 =%f", [self class], cell.frame.size.width);

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
        NSLog(@"%@ cellForRowAtIndexPath cell.frame.size.width 2 =%f", [self class], cell.frame.size.width);
        [cell setFrame:CGRectMake(0.f, 0.f, tableView.frame.size.width, tableView.frame.size.height)];
    }
    NSLog(@"%@ cellForRowAtIndexPath cell.frame.size.width 3 =%f", [self class], cell.frame.size.width);

1 則留言: