Я использую cell.contentView.bounds.size.width
для вычисления положения текстового поля в ячейке UITableView. Когда ячейка создана, код отладки сообщает о ширине как 302. Когда ячейка прокручивается за пределы экрана и затем снова включается, код отладки сообщает, что она составляет 280 - каждый раз. Похоже, он не хочет возвращаться к 302 и остается застрявшим на 280. Конечный результат заключается в том, что текстовое поле помещается в неправильное место во второй раз, когда поле помещается в contentView ячейки, хотя оно было помещено в правильное место в первый раз.
Я думаю, что цифра 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;
}
В код по умолчанию необходимо внести только два изменения: изменение числа строк в разделе («return 5») и стиль ячейки должны быть 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 долларов (даже без работающего приложения), чтобы я мог пригласить кого-нибудь из Apple в этот раз.
У кого-нибудь есть идеи, что здесь происходит?
Спасибо!
Хотите увидеть что-нибудь более интересное? Попробуйте это вместо строки static NSString...
:
NSString *CellIdentifier = [NSString stringWithFormat: @"%d", arc4random() ];
NSLog(@"%@",CellIdentifier);
Теперь, каждый раз, ширина в журнале равна всегда 302. Тогда может показаться, что повторно используемая ячейка имеет ширину содержимого, отличную от исходной ячейки.
Опять ... интересно ... кто-нибудь понял?