2

さまざまなUITableViewセクションと行があります。各セルをアイコンのように異なるラベルまたは画像にしたいのですが、なぜこの写真のように見えるのかわかりません:

また、スクロールするとラベルのサイズが変更されました!

コードは次のとおりです。

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

NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]];
NSArray *contents = [[self sectionContents] objectForKey:key];
NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]];

static NSString *CellIdentifier = @"CellIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  
reuseIdentifier:CellIdentifier];
}
if(indexPath.row == 0)
{
    UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(3,0,40,40)];
    label.backgroundColor = [UIColor darkGrayColor];
    [cell.contentView addSubview:label];

}
else if(indexPath.row == 1)
{
    UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(3,0,40,40)];
    label.backgroundColor = [UIColor redColor];
    [cell.contentView addSubview:label];    }
else if(indexPath.row == 2)
{
    UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(3,0,40,40)];
    label.backgroundColor = [UIColor redColor];
    [cell.contentView addSubview:label];    }
else{

}
[[cell textLabel] setText:contentForThisRow];
return cell;
}
4

2 に答える 2

0

これ以上のカスタマイズが必要でない限り、セルをサブクラス化する必要はありません。試してみましょう:

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

    NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]];
    NSArray *contents = [[self sectionContents] objectForKey:key];
    NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]];

    static NSString *CellIdentifier = @"CellIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  
    reuseIdentifier:CellIdentifier];
        // If you need to add more custom views then create them here. E.g.: 
        UILabel *myLable = [[UILabel alloc] initWithFrame:CGRectMake(10,200,20,20)];
        myLable.text = @"Some Value";
        label.tag = 101; // use anything but 1. Better use a constant expression.
        [cell.contentView addSubview:myLable];
    }
    switch (indexPath.row) {
      case 1: 
        cell.imageView.image = [UIImage imageNamed:@"red.png"];
        break;

      case 2: 
        cell.imageView.image = [UIImage imageNamed:@"blue.png"];
        break;

      case 3: 
        cell.imageView.image = [UIImage imageNamed:@"green.png"];
        break;
    }  

   UILabel* theLabel = (UILabel *) [cell viewWithTag:101]; // again, a constant is of better style here. 
   if (theLabel  { // it must exist. However, it is good practice to check upon that. 
    label.text = @"someOtherValue";
   }

    [[cell textLabel] setText:contentForThisRow];

    // you may need to layout your subviews here. 

    return cell;
}
于 2012-08-07T12:07:05.513 に答える
0

あなたのコードでは、ユーザーがテーブルをスクロールすると、ラベルが何度も作成されます! カラーラベルがセルに既に存在するかどうかを確認してから、色を設定する必要があります

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
int colorLabelTag = 9999;
if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  
                                  reuseIdentifier:CellIdentifier];     
}

// Because table cells are reusing we must check is label already in cell
UILabel* label = (UILabel *) [cell viewWithTag:colorLabelTag];
// If no - we create label
if (!label) {
    UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(3,0,40,40)];
    label.tag = colorLabelTag;
    [cell addSubview:label];
}

if(indexPath.row == 0)
{
    label.backgroundColor = [UIColor darkGrayColor];        
}
else if(indexPath.row == 1)
{
    label.backgroundColor = [UIColor redColor];
}
else if(indexPath.row == 2)
{
    label.backgroundColor = [UIColor redColor];
}
[[cell textLabel] setText:contentForThisRow];
return cell;
于 2012-08-07T09:25:06.163 に答える