我使用cell.contentView.bounds.size.width
來計算在一個UITableView細胞中的文本字段的位置。當單元格被創建時,調試代碼將寬度報告爲302.當單元格滾動屏幕然後再次打開時,調試代碼每次報告它是280。它似乎不想回到302並停留在280.最終的結果是,第二次將字段放入單元格的contentView時,文本字段被放置在錯誤的位置,儘管它放在了第一次正確的地方。的UITableView cell.contentView.bounds.size.width改變與小區重用
我圖22是顯著莫名其妙,但我不知道它是什麼。猜測它可能是披露箭頭,我在寬度測定的前面移動了「清除單元」代碼,包括將配件設置爲nada。
有人可以告訴我這裏發生了什麼事嗎?
( - 這是我所知道的 - 與不相關的東西剪掉了)的代碼如下所示:
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
while([cell.contentView.subviews count]){
id subview = [cell.contentView.subviews objectAtIndex:0];
[subview removeFromSuperview];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
8< snip!
CGFloat theCellWidth = cell.contentView.bounds.size.width - 44.0;
CGFloat theLineHeight = [[UIFont boldSystemFontOfSize: [UIFont labelFontSize]+1.0] lineHeight];
NSLog(@"cell.contentView.bounds.size.width %1.0f",cell.contentView.bounds.size.width);
if (0==section) {
switch (row) {
case 2:
while([cell.contentView.subviews count]){
id subview = [cell.contentView.subviews objectAtIndex:0];
[subview removeFromSuperview];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.text = @" ";
cell.detailTextLabel.text = @"The Age";
theAgeTextField.frame = CGRectMake(10.0, 2.0, theCellWidth, theLineHeight);
// NSLog(@"cell.contentView %@",cell.contentView);
theAgeTextField.text = theAge;
theAgeTextField.font = [UIFont boldSystemFontOfSize: [UIFont labelFontSize]+1.0];
theAgeTextField.keyboardType = UIKeyboardTypeDecimalPad;
theAgeTextField.borderStyle = UITextBorderStyleNone;
theAgeTextField.userInteractionEnabled = NO;
[cell.contentView addSubview:theAgeTextField];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
break;
8< snip! (lots of closing braces and other stuff omitted)
return cell;
}
想試試這一個人在家,男孩和女孩?
開始使用新的基於導航的應用。將下面的代碼放到RootViewController.m:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
NSLog(@"cell.contentView.bounds.size.width %1.0f",cell.contentView.bounds.size.width);
// Configure the cell.
return cell;
}
只有兩個來實現這一目標所需的默認代碼變化:改變部分的行數(「返回5」)和的風格cell必須是UITableViewCellStyleSubtitle。然後,當你運行程序,你會看到這五線:
2011-06-18 11:10:19.976 TestTableCells[7569:207] cell.contentView.bounds.size.width 302
2011-06-18 11:10:19.978 TestTableCells[7569:207] cell.contentView.bounds.size.width 302
2011-06-18 11:10:19.979 TestTableCells[7569:207] cell.contentView.bounds.size.width 302
2011-06-18 11:10:19.980 TestTableCells[7569:207] cell.contentView.bounds.size.width 302
2011-06-18 11:10:19.982 TestTableCells[7569:207] cell.contentView.bounds.size.width 302
將一些細胞關閉屏幕(拖上 - 下沒有做任何事情),當他們再次出現,你得到這個:
2011-06-18 11:10:24.013 TestTableCells[7569:207] cell.contentView.bounds.size.width 320
2011-06-18 11:10:24.047 TestTableCells[7569:207] cell.contentView.bounds.size.width 320
2011-06-18 11:10:24.130 TestTableCells[7569:207] cell.contentView.bounds.size.width 320
坦率地說,我很沮喪與此心裏很不舒服,而且我這樣很動心從腰包裏掏出$ 99(無工作程序,甚至),所以我可以有有人在蘋果權衡這一點。
任何人有什麼是怎麼回事任何想法?
謝謝!
想看看更有趣的東西嗎?現在
NSString *CellIdentifier = [NSString stringWithFormat: @"%d", arc4random() ];
NSLog(@"%@",CellIdentifier);
,每一次,在日誌中的寬度是始終這似乎,那麼,重用細胞有不同的內容寬度比原來的電池:代替static NSString...
線試試這個。
再次...不知道...任何人都得到了線索?
我注意到了一個類似的問題,據此我使用UITableViewCell邊界來創建一個CAGradientLayer來爲每個單元格添加一個漸變。當iPad處於縱向模式時,我看到單元格右側的白色方框,單元格的寬度似乎註冊爲橫向方向寬度。表格滾動和單元格重新使用後,單元格將註冊爲縱向並選取正確的寬度。仍在尋找如何解決它! – robotpukeko 2012-07-18 09:16:39