2013-07-13 35 views
0

我有一個tableview表示一個feed,有三個不同的自定義UITableView單元格。其中一個(最上面的一個)是堅實的,應該始終在那裏,但是單元不足以表明其中一個是產品或事件單元(從DB加載)。事情是Eventcells有一個textview和一個imageview可以在高度varie,所以要正確查看這些我計算正確的高度,然後在heightForRowAtIndexPath中設置高度。我需要以某種方式更新單元格的高度,所以我做了一個tableview開始/結束更新。但是,當我爲每個單元執行此操作時,每當它加載到視圖中時,所有單元格都會開始反彈,並在滾動tableview時更改內容。UITableViewCell:開始/結束更新導致單元格翻轉

這裏是我的cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
if (indexPath.section == 0) { 
    return [self loadJobInfoCell:indexPath]; 

} else if (indexPath.section == 1) { 
    if ([[jobDetailsArray objectAtIndex:indexPath.row] isKindOfClass:[JobProduct class]]) { 
     return [self loadProductCell:indexPath]; 
    } else if ([[jobDetailsArray objectAtIndex:indexPath.row] isKindOfClass:[JobEvent class]]) { 
      static NSString *CellIdentifier = @"EventCell"; 
EventCell *cell = [tableViewRef dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

if (cell == nil) { 
    cell = [[EventCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 

JobEvent *currentEvent = [jobDetailsArray objectAtIndex:indexPath.row]; 
// setting labels and stuff here 

// Is there an image to this event? 
if (![currentEvent.EventPicture isEqual:[NSNull null]]) { 
    [[cell largeImage] setImage:currentEvent.EventPicture]; 
    [[cell largeImageHeightConstraint] setConstant:currentEvent.EventPicture.size.height]; 

    NSNumber *height = [NSNumber numberWithFloat:currentEvent.EventPicture.size.height]; 
    [largeImagesDictionary setObject:height forKey:[NSString stringWithFormat:@"%d", indexPath.row]]; 
} else { 
    [[cell largeImageHeightConstraint] setConstant:0.f]; 
} 

    // set correct height for the textview 
NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14]}; 
CGRect paragraphRect = [cell.tvText.text boundingRectWithSize:CGSizeMake(204.f, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:attributes context:nil]; 

[[cell tvTextHeightConstraint] setConstant:paragraphRect.size.height+16.f]; 

NSNumber *height = [NSNumber numberWithFloat:[[cell tvTextHeightConstraint] constant]]; 
[eventTextHeightDictionary setObject:height forKey:[NSString stringWithFormat:@"%d", indexPath.row]]; 
[tableViewRef beginUpdates]; 
[tableViewRef endUpdates]; 
return cell; 
    } 
} 
return nil; 

沒有開始/ endupdates它工作正常,但細胞不是正確的高度,並得到削減的。我能以某種方式更新高度而無需重新加載表格,還是有更好的解決方案來處理整個情況?我試圖跟蹤哪些細胞已經獲得了更新,但這並不起作用,但它仍然弄亂了順序,高度和內容。我已經嘗試過所有我可能想到的解決方案組合,但作爲一名新手iOS開發人員,我甚至不確定是否會採取正確的方法解決此問題。

非常感謝。

編輯: 男人,我很愚蠢..我坐下來計算高度,在cellforrowatindex中放在heightforrowatindex中,並通過中間數據傳遞數據。我用自動佈局解決了這個問題,並在heightforrowatindex中預先計算了數據的高度。

回答

1

我不完全確定你的設置,但是我這樣做的方式是在IB中設置約束條件,以便圖像視圖和文本視圖能夠隨着單元格自動擴展。通過這樣做,我不必對代碼中的圖像視圖或文本視圖進行任何大小更改,只需對單元大小進行更改。我的小區建立看起來像這樣:

enter image description here

的圖像視圖在x方向爲中心,並具有約束到電池的頂部和一個從底部到文本視圖的頂部。文本視圖對單元格的邊和底部有約束。我在圖像視圖中放置了一個虛擬圖像,並從「編輯器」菜單中選擇「尺寸適合內容」 - 這會導致圖像視圖的高度和寬度限制被刪除。

在代碼中,我計算圖像視圖和文本視圖的大小,然後在heightForRowAtIndexPath中返回它們高度的總和(加上一個模糊因子)。這裏是一個示例應用程序的代碼:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.theData = @[@{@"text":@"jkkjhkj kh k jk h hkj hjkhkjh hjkh jk hhkjhjkh jkh hkj hkjh hkjhjkhhkk jk jkh jkhkhkjhjhkjhkjhkkjhjjhk kjhkjh jkh hk h kj h jkh jkh kjh kh hjkhk jhjk", @"Pic":@"pic1.jpg"},@{@"text":@"fjhg lfkgh gjk gjk glkjfhgjkhgjkgh sjkghsjkgsjgjgk jgk hg hdgjlhjhjgjg fgjklfg fghjgk gjlkg hjgh jg jlkgljsdkggjlglgjdlkg hgjlgjfkghjg ljhfg jlskfdg hjgjlkgjlkdf gjfghjlkfgljkgjlkdgjdfghjdgjglhjkg hljkg ljkgljkfgljkgljksdgljkfgjlfg ljfglldkfjgh ljkgjlkf dgfghslfjdgklfjgljfdfgl", @"Pic":@"pic2.tiff"},@{@"text":@"jdkh lj flfh ljs fajlh ljds f", @"Pic":@"pic3.tiff"}]; 
    [self.tableView reloadData]; 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return self.theData.count; 
} 

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    CGSize textViewSize = [self.theData[indexPath.row][@"text"] sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(280.f, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping]; 
    UIImage *pic = [UIImage imageNamed:self.theData[indexPath.row][@"Pic"]]; 
    CGSize imageViewSize = pic.size; 
    return textViewSize.height + imageViewSize.height + 40; 
} 

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

    RDCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 
    cell.tv.text = self.theData[indexPath.row][@"text"]; 
    cell.iv.image = [UIImage imageNamed:self.theData[indexPath.row][@"Pic"]]; 
    return cell; 
} 
+0

謝謝你的答覆!我會嘗試使用AutoLayout進行設置並返回給您。 –

+0

這幫助我達成了一個可行的解決方案,請參閱第一篇文章。謝謝您的幫助! –

相關問題