добавление различных UILabel для каждой ячейки в UITableView - PullRequest
0 голосов
/ 23 июля 2011

Я добавляю различный текст в каждую ячейку в UITableView.Однако, когда я это делаю, тест отображается в каждой ячейке, кроме первой. Так, если есть массив с 9 числами от 1 до 9, 1 отображается во второй ячейке, 2 отображается в третьей ячейке и, соответственно.В первой ячейке ничего не показано. Вот коды

// 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:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    cell = [self getCellContentView:CellIdentifier];
}
if (indexPath.row == 0) {
    NSLog(@"hi");
}
//add another textfield
NSString *path = [[NSBundle mainBundle] pathForResource:@"Rankvalue" ofType:@"plist"];
NSMutableArray* rank = [[NSMutableArray alloc] initWithContentsOfFile:path];
NSString *rankValue = [rank objectAtIndex:indexPath.row];

UILabel *rankLabel = (UILabel *)[cell viewWithTag:1];
rankLabel.text = rankValue;
[rankLabel setFont:[UIFont fontWithName:@"austin power" size:[@"40" intValue]]];

CGRect labelFrame = CGRectMake(220,70,50,40.0);
[rankLabel setFrame:labelFrame];

// Configure the cell(thumbnail).
cell.textLabel.text = [self.stars objectAtIndex:indexPath.row];
NSString *path2 = [[NSBundle mainBundle] pathForResource:@"Filename" ofType:@"plist"];
self.filename = [[NSMutableArray alloc] initWithContentsOfFile:path2];
cell.imageView.image = [UIImage imageNamed:[self.filename objectAtIndex:indexPath.row]];

//transparent cell
cell.backgroundColor = [UIColor clearColor];

[rankLabel release];
[rankValue release];

return cell;
}

А это код подпредставления ячейки

- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier {

CGRect CellFrame = CGRectMake(0, 0, 320, 65);
CGRect Label1Frame = CGRectMake(17,5,250,18);  

UILabel *lblTemp;


UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:cellIdentifier] autorelease];  
lblTemp = [[UILabel alloc] initWithFrame:Label1Frame];
[lblTemp setFont:[UIFont fontWithName:@"Arial-Bold" size:15]];
lblTemp.tag = 1;
lblTemp.backgroundColor=[UIColor clearColor];
lblTemp.numberOfLines=0;
[cell.contentView addSubview:lblTemp];    

return cell;
}

Ответы [ 2 ]

0 голосов
/ 24 июля 2011

initWithFrame:reuseIdentifier: устарел для initWithStyle:reuseIdentifier:

Вы должны создать один из стандартных стилей.

Устанавливать нужные значения (шрифт и т. Д.) При создании ячейки, а нена обновление.Рамка текстового поля одинакова.И цвет фона.

Вы действительно не хотите перезагружать этот лист (о, их два!) Каждый раз, когда вы обновляете ячейку!Загрузите его один раз другим способом и кэшируйте как ivar.

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  static NSString *CellIdentifier = @"Cell";

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil) {
    // create and set up all the layout attributes of the cell
    // it should be auto released
    // which should include a call like the following, or loading a cell from a nib
    // cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
    //                               reuseIdentifier:CellIdentifier];
    // [cell autorelease];
  }

  // the only thing that should happen here is setting the data values
  // into the cell!!!!!!  All these values should be loaded once and
  // kept as "model state" by the controller.  Do not create an image
  // each time through, do not load an entire plist to access one item
  // each time through

  rankLabel.text = rankValue;
  cell.textLabel.text = [self.stars objectAtIndex:indexPath.row];
  cell.imageView.image = [self imageAtRow:indexPath.row];


}

Если отображаемое изображение отличается для каждой ячейки, использование imageNamed: не подходит.Если есть 2 или 3 изображения, которые указывают тип ячейки, каждое из которых будет использоваться часто, вероятно, предпочтительным будет imageNamed:.imageWithContentsOfFile: - это другой вариант, вам нужно построить полный путь

0 голосов
/ 23 июля 2011

Попробуйте переключить это:

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    //cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    cell = [self getCellContentView:CellIdentifier];
}

для этого:

UITableViewCell *cell=[self getCellContentView:CellIdentifier];

Не говорите, что это сработает, но просто сделайте это.Кроме того, сделайте это в вашем getCellContentView:

[lblTemp release]

Или у вас будет утечка.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...