2011-12-19 4 views
1

2つのUILabels、1タイトル1サブタイトル、 segmentedControlが選択されたときに変更されることを意味します。UITableViewCellでオーバーラップするUILabel

別のセグメントが選択されても、同じUILabelが重なって表示されるのですか?

セルに再表示される前にスーパービューからラベルを削除するアクションを作成する必要があると思いますか?ちょうど行くためにか疑問については

2 Labels overlapping themselves

- (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]; 
    } 

    EventUpcoming *aEventUpcoming = [euEvent objectAtIndex:indexPath.section]; 
    EventWeekly *aEventWeekly = [ewEvent objectAtIndex:indexPath.section]; 

    UILabel *cellTitle = [[UILabel alloc] initWithFrame:CGRectMake(15, 5, 290, 20)]; 
    UILabel *cellSubtitle = [[UILabel alloc] initWithFrame:CGRectMake(15, 20, 290, 20)]; 

    NSString *titleString = [[NSString alloc] init]; 
    NSString *subtitleString = [[NSString alloc] init]; 

    if (segmentedControl.selectedSegmentIndex == 0) 
    { 
     Event *aEvent = [aEventUpcoming.event objectAtIndex:indexPath.row]; 
     titleString = aEvent.name; 
     subtitleString = aEvent.subtitle; 
    } 
    else 
    { 
     Event *aEvent = [aEventWeekly.event objectAtIndex:indexPath.row]; 
     titleString = aEvent.name; 
     subtitleString = aEvent.subtitle; 
    } 

    NSString *titleStringUC = [titleString uppercaseString]; 
    NSString *subtitleStringLC = [subtitleString lowercaseString]; 

    cellTitle.text = titleStringUC; 
    cellTitle.font = [UIFont boldSystemFontOfSize:11]; 
    cellTitle.textColor = [UIColor colorWithRed:142/255.0f green:142/255.0f blue:142/255.0f alpha:1]; 
    cellTitle.shadowColor = [UIColor whiteColor]; 
    cellTitle.shadowOffset = CGSizeMake(1, 1); 
    cellTitle.backgroundColor = [UIColor clearColor]; 

    cellSubtitle.text = subtitleStringLC; 
    cellSubtitle.font = [UIFont boldSystemFontOfSize:11]; 
    cellSubtitle.textColor = [UIColor colorWithRed:177/255.0f green:177/255.0f blue:177/255.0f alpha:1]; 
    cellSubtitle.shadowColor = [UIColor whiteColor]; 
    cellSubtitle.shadowOffset = CGSizeMake(1, 1); 
    cellSubtitle.backgroundColor = [UIColor clearColor]; 


    tableViewCellSeparator = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TableCellSeparator.png"]]; 
    tableViewCellSeparator.frame = CGRectMake(0, cell.bounds.size.height - 2, 320, 2); 

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

    [cell.contentView addSubview:cellTitle]; 
    [cell.contentView addSubview:cellSubtitle]; 
    [cell.contentView addSubview:tableViewCellSeparator]; 






    return cell; 
} 

UPDATE:どちらも、非常に有効な回答があっ
、tyvm

答えて

0

.....

- (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]; 


    EventUpcoming *aEventUpcoming = [euEvent objectAtIndex:indexPath.section]; 
    EventWeekly *aEventWeekly = [ewEvent objectAtIndex:indexPath.section]; 

    UILabel *cellTitle = [[UILabel alloc] initWithFrame:CGRectMake(15, 5, 290, 20)]; 
    UILabel *cellSubtitle = [[UILabel alloc] initWithFrame:CGRectMake(15, 20, 290, 20)]; 

    NSString *titleString = [[NSString alloc] init]; 
    NSString *subtitleString = [[NSString alloc] init]; 

    NSString *titleStringUC = [titleString uppercaseString]; 
    NSString *subtitleStringLC = [subtitleString lowercaseString]; 

    cellTitle.text = titleStringUC; 
    cellTitle.font = [UIFont boldSystemFontOfSize:11]; 
    cellTitle.textColor = [UIColor colorWithRed:142/255.0f green:142/255.0f blue:142/255.0f alpha:1]; 
    cellTitle.shadowColor = [UIColor whiteColor]; 
    cellTitle.shadowOffset = CGSizeMake(1, 1); 
    cellTitle.backgroundColor = [UIColor clearColor]; 
    cellTitle.tag = 10; 

    cellSubtitle.text = subtitleStringLC; 
    cellSubtitle.font = [UIFont boldSystemFontOfSize:11]; 
    cellSubtitle.textColor = [UIColor colorWithRed:177/255.0f green:177/255.0f blue:177/255.0f alpha:1]; 
    cellSubtitle.shadowColor = [UIColor whiteColor]; 
    cellSubtitle.shadowOffset = CGSizeMake(1, 1); 
    cellSubtitle.backgroundColor = [UIColor clearColor]; 
    cellSubtitle.tag = 11; 


    tableViewCellSeparator = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TableCellSeparator.png"]]; 
    tableViewCellSeparator.frame = CGRectMake(0, cell.bounds.size.height - 2, 320, 2); 

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 


    [cell.contentView addSubview:cellTitle]; 
    [cell.contentView addSubview:cellSubtitle]; 
    [cell.contentView addSubview:tableViewCellSeparator]; 
} 

cellTitle = (UILabel *)[cell.contentView viewWithTag:10]; 
cellSubtitle = (UILabel *)[cell.contentView viewWithTag:11]; 

if (segmentedControl.selectedSegmentIndex == 0) 
{ 
    Event *aEvent = [aEventUpcoming.event objectAtIndex:indexPath.row]; 
    titleString = aEvent.name; 
    subtitleString = aEvent.subtitle; 
} 
else 
{ 
    Event *aEvent = [aEventWeekly.event objectAtIndex:indexPath.row]; 
    titleString = aEvent.name; 
    subtitleString = aEvent.subtitle; 
} 

return cell; 
} 
3

あなたが正しくあなたのセルを再利用していない、あなたので、再利用のパフォーマンス上のメリットが得られず、コードが複雑になります。あなたは、Appleがあなたに提供してくれたものをそのまま使ってはいません。

最初に、cell==nilブロック内にすべてのサブビューを作成して追加する必要があります。ここで再利用可能なセルを作成します。残りのルーチンでは、セルをに再設定するだけで、になります。これははるかに高速です。

第2に、UITableViewCellには既に2つのラベルが組み込まれています。作成する必要はありません。それらはtextLabeldetailTextLabelと呼ばれます。彼らは通常のラベルです。あなたはそれらを動かすことができ、好きなように見せることができます。 cell==nilブロックでそれを行います。

ブロックブロックの外にcell.textLabel.text = ...cell.detailTextLabel.text = ...を呼び出すだけでいいです。

2つ以上のラベルが必要な場合は、UITableViewCellをサブクラス化して新しいプロパティを作成して、セルを簡単に再構成できるようにします。私はそれがこれをnow.Try働くことを願っています

関連する問題