簡體   English   中英

不同部分行中的iOS Tableview單元具有相同的布局

[英]iOS Tableview cell in different section row has the same layout

我有一個問題,描述它的最好方法是給你看照片。 該部分的最后一行應具有光澤的樣式和文本對齊的中心。 首先我有一張桌子,上面有一個部分:

http://image-upload.de/image/RL93lJ/58f2422d97.png

但是當我在頂部添加另一部分時,我得到的是這樣的:

http://image-upload.de/image/JggrVq/6ee42a1089.png

這是短代碼:

static NSString *CellIdentifier = @"CellOrder";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cellIsLastOne == TRUE) {

        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
        //name


        cell.textLabel.text = @"Bezahlt";
        cell.textLabel.textAlignment = NSTextAlignmentCenter;

        cell.textLabel.font =[UIFont fontWithName:@"Avenir" size:25];

        cell.textLabel.textColor =  [UIColor whiteColor];

        //Glossy
        CALayer *thisLayer = cell.layer;
        if (thisLayer.sublayers.count == 1) {
            CAGradientLayer *glossLayer = [CAGradientLayer layer];
            glossLayer.frame = CGRectMake(cell.bounds.origin.x + cellOffset, cell.bounds.origin.y, self.tableView.frame.size.width - cellOffset * 2, cellItemHeight);
            glossLayer.colors = [NSArray arrayWithObjects:
                                 (id)[UIColor colorWithWhite:1.0f alpha:0.4f].CGColor,
                                 (id)[UIColor colorWithWhite:1.0f alpha:0.2f].CGColor,
                                 (id)[UIColor colorWithWhite:0.75f alpha:0.0f].CGColor,
                                 (id)[UIColor colorWithWhite:1.0f alpha:0.2f].CGColor,
                                 nil];
            glossLayer.locations = [NSArray arrayWithObjects:
                                    [NSNumber numberWithFloat:0.0f],
                                    [NSNumber numberWithFloat:0.5f],
                                    [NSNumber numberWithFloat:0.5f],
                                    [NSNumber numberWithFloat:1.0f],
                                    nil];
            [thisLayer addSublayer:glossLayer];
        }

    } else {
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        ItemID;

        cell.textLabel.text = @"Name";
        cell.detailTextLabel.text = @"Price";
        cell.detailTextLabel.textColor = [UIColor whiteColor];


        cell.textLabel.font =[UIFont fontWithName:@"Avenir" size:20];
        cell.detailTextLabel.font =[UIFont fontWithName:@"Avenir" size:20];

        cell.textLabel.textColor =  [UIColor whiteColor];

    }

    cell.detailTextLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.4];
    cell.textLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.4];

    cell.detailTextLabel.shadowOffset = CGSizeMake(0, 2);
    cell.textLabel.shadowOffset = CGSizeMake(0, 2);

    cell.backgroundColor = [UIColor colorWithRed:0.72 green:0.76 blue:0.03 alpha:1.0];
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.detailTextLabel.backgroundColor = [UIColor clearColor];

    return cell;

我真的沒有問題。 請幫我。 謝謝! ;)

好吧,您的屏幕截圖顯示的問題表明您對cellIsLastOne的計算是錯誤的。

但是,即使是正確的,當您在表格視圖中滾動時,也會有很多問題,因為正在重復使用單元格。 要解決此問題,您可以:

  • 對光澤單元使用不同的重復使用標識符
  • 刪除else塊中的光澤子層

基本上,當您調用dequeueReusableCellWithIdentifier: ,它可能會返回您之前向其添加光澤層的單元格。 此外,這可能會多次添加光澤層。

我編輯您的代碼可能會對您有所幫助。

    static NSString *CellIdentifier = @"CellOrder";
    static NSString *glowCellIdentifier = @"glowCellOrder";
    UITableViewCell *cell = nil;
    if (cellIsLastOne == TRUE) {
        cell = [tableView dequeueReusableCellWithIdentifier:glowCellIdentifier];
    } else {
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    }

     if (cell == nil) {
          if (cellIsLastOne == TRUE) {
             cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:glowCellIdentifier];  
             cell.selectionStyle = UITableViewCellSelectionStyleGray;
          } else {
             cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
             cell.selectionStyle = UITableViewCellSelectionStyleNone;
          }
            cell.accessoryType = UITableViewCellAccessoryNone;
        }

    if (cellIsLastOne == TRUE) {
        //name

        cell.textLabel.text = @"Bezahlt";
        cell.textLabel.textAlignment = NSTextAlignmentCenter;

        cell.textLabel.font =[UIFont fontWithName:@"Avenir" size:25];

        cell.textLabel.textColor =  [UIColor whiteColor];

        //Glossy
        CALayer *thisLayer = cell.layer;
        if (thisLayer.sublayers.count == 1) {
            CAGradientLayer *glossLayer = [CAGradientLayer layer];
            glossLayer.frame = CGRectMake(cell.bounds.origin.x + cellOffset, cell.bounds.origin.y, self.tableView.frame.size.width - cellOffset * 2, cellItemHeight);
            glossLayer.colors = [NSArray arrayWithObjects:
                                 (id)[UIColor colorWithWhite:1.0f alpha:0.4f].CGColor,
                                 (id)[UIColor colorWithWhite:1.0f alpha:0.2f].CGColor,
                                 (id)[UIColor colorWithWhite:0.75f alpha:0.0f].CGColor,
                                 (id)[UIColor colorWithWhite:1.0f alpha:0.2f].CGColor,
                                 nil];
            glossLayer.locations = [NSArray arrayWithObjects:
                                    [NSNumber numberWithFloat:0.0f],
                                    [NSNumber numberWithFloat:0.5f],
                                    [NSNumber numberWithFloat:0.5f],
                                    [NSNumber numberWithFloat:1.0f],
                                    nil];
            [thisLayer addSublayer:glossLayer];
        }

    } else {       

        ItemID;

        cell.textLabel.text = @"Name";
        cell.detailTextLabel.text = @"Price";
        cell.detailTextLabel.textColor = [UIColor whiteColor];


        cell.textLabel.font =[UIFont fontWithName:@"Avenir" size:20];
        cell.detailTextLabel.font =[UIFont fontWithName:@"Avenir" size:20];

        cell.textLabel.textColor =  [UIColor whiteColor];

    }

    cell.detailTextLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.4];
    cell.textLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.4];

    cell.detailTextLabel.shadowOffset = CGSizeMake(0, 2);
    cell.textLabel.shadowOffset = CGSizeMake(0, 2);

    cell.backgroundColor = [UIColor colorWithRed:0.72 green:0.76 blue:0.03 alpha:1.0];
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.detailTextLabel.backgroundColor = [UIColor clearColor];

    return cell;

此代碼可有效地重用單元並節省內存。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM