Using a cached UIView to set the background view of a cell in tableView: willDisplayCell: forRowAtIndexPath:

This is my solution for setting custom background elements of table cell of grouped tables:

- (UIView *)top
{
    if (_top) {
        return _top;
    }

    _top = [[UIView alloc] init];
    [_top setBackgroundColor:[UIColor blueColor]];

    return _top;
}

// dot dot dot

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger section = [indexPath section];
    NSInteger row = [indexPath row];
    NSInteger maxRow = [tableView numberOfRowsInSection:section] - 1;

    if (maxRow == 0) {
        [cell setBackgroundView:[self lonely]];
    } else if (row == 0) {
        [cell setBackgroundView:[self top]];
    } else if (row == maxRow) {
        [cell setBackgroundView:[self bottom]];
    } else {
        [cell setBackgroundView:[self middle]];
    }
}

Obviously, this does not work as expected, which brings me here, but it works when I do not use cached views:

UIView *background = [[UIView alloc] init];

if (maxRow == 0) {
    [background setBackgroundColor:[UIColor redColor]];
} else if (row == 0) {
    [background setBackgroundColor:[UIColor blueColor]];
} else if (row == maxRow) {
    [background setBackgroundColor:[UIColor yellowColor]];
} else {
    [background setBackgroundColor:[UIColor greenColor]];
}

[cell setBackgroundView:background];

UPDATE: After Jonathan pointed out that I cannot use the same view for multiple cells, I decided to follow the table view model where she has a queue of reusable cells. For my implementation, I have a queue of reusable background views ( _backgroundViewPool):

@implementation RootViewController {
    NSMutableSet *_backgroundViewPool;
}
- (id)initWithStyle:(UITableViewStyle)style
{
    if (self = [super initWithStyle:style]) {
        _backgroundViewPool = [[NSMutableSet alloc] init];

        UITableView *tableView = [self tableView];
        [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    }

    return self;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 6;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.

    if (section == 0) {
        return 1;
    }

    return 10;
}

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

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    [[cell textLabel] setText:[NSString stringWithFormat:@"[%d, %d]", [indexPath section], [indexPath row]]];

    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIView *backgroundView = [cell backgroundView];
    [_backgroundViewPool addObject:backgroundView];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger section = [indexPath section];
    NSInteger row = [indexPath row];
    NSInteger maxRow = [tableView numberOfRowsInSection:section] - 1;
    UIColor *color = nil;


    if (maxRow == 0) {
        // single cell
        color = [UIColor blueColor];
    } else if (row == 0) {
        // top cell
        color = [UIColor redColor];
    } else if (row == maxRow) {
        // bottom cell
        color = [UIColor greenColor];
    } else {
        // middle cell
        color = [UIColor yellowColor];
    }

    UIView *backgroundView = nil;

    for (UIView *bg in _backgroundViewPool) {
        if (color == [bg backgroundColor]) {
            backgroundView = bg;
            break;
        }
    }

    if (backgroundView) {
        [backgroundView retain];
        [_backgroundViewPool removeObject:backgroundView];
    } else {
        backgroundView = [[UIView alloc] init];
        [backgroundView setBackgroundColor:color];
    }

    [cell setBackgroundView:[backgroundView autorelease]];
}

, . ! , , , , , , .


, . , . , , XJones jszumski, , (, , , ).

+5
9

4 "", "", "" "" backgroundView ? UITableView .


: UITableViewController, ( Espresso). tableView:willDisplayCell:forRowAtIndexPath: tableView:didDisplayCell:forRowAtIndexPath: , backgroundViewForStyle:.

typedef NS_ENUM(NSInteger, JSCellBackgroundStyle) {
    JSCellBackgroundStyleTop = 0,
    JSCellBackgroundStyleMiddle,
    JSCellBackgroundStyleBottom,
    JSCellBackgroundStyleSolitary
};

@implementation JSMasterViewController {
    NSArray *backgroundViewPool;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    // these mutable arrays will be indexed by JSCellBackgroundStyle values
    backgroundViewPool = @[[NSMutableArray array],  // for JSCellBackgroundStyleTop
                           [NSMutableArray array],  // for JSCellBackgroundStyleMiddle
                           [NSMutableArray array],  // for JSCellBackgroundStyleBottom
                           [NSMutableArray array]]; // for JSCellBackgroundStyleSolitary
}


#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 5;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 2) {
        return 1;

    } else if (section == 3) {
        return 0;
    }

    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;

    static NSString *switchCellIdentifier = @"switchCell";
    static NSString *textFieldCellIdentifier = @"fieldCell";
    static NSString *textCellIdentifier = @"textCell";

    UITableViewCell *cell = nil;

    // apply a cached cell type (you would use your own logic to choose types of course)
    if (row % 3 == 0) {
        cell = [tableView dequeueReusableCellWithIdentifier:switchCellIdentifier];

        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:switchCellIdentifier];

            UISwitch *someSwitch = [[UISwitch alloc] init];
            cell.accessoryView = someSwitch;

            cell.textLabel.text = @"Switch Cell";
        }

    } else if (row % 3 == 1) {
        cell = [tableView dequeueReusableCellWithIdentifier:textFieldCellIdentifier];

        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:textFieldCellIdentifier];

            UITextField *someField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 80, 30)];
            someField.borderStyle = UITextBorderStyleRoundedRect;
            cell.accessoryView = someField;

            cell.textLabel.text = @"Field Cell";
        }

    } else {
        cell = [tableView dequeueReusableCellWithIdentifier:textCellIdentifier];

        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:textCellIdentifier];

            cell.textLabel.text = @"Generic Label Cell";
        }
    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"[%d, %d]", section, row];
    cell.detailTextLabel.backgroundColor = [UIColor clearColor];

    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    // apply a cached background view
    JSCellBackgroundStyle backgroundStyle = [self backgroundStyleForIndexPath:indexPath tableView:tableView];
    cell.backgroundView = [self backgroundViewForStyle:backgroundStyle];
}

- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    JSCellBackgroundStyle backgroundStyle = [self backgroundStyleForIndexPath:indexPath tableView:tableView];
    NSMutableArray *stylePool = backgroundViewPool[backgroundStyle];

    // reclaim the background view for the reuse pool
    [cell.backgroundView removeFromSuperview];

            if (cell.backgroundView != nil) {
            [stylePool addObject:cell.backgroundView];
            }

    cell.backgroundView = nil; // omitting this line will cause some rows to appear without a background because they try to be in two superviews at once
}

- (JSCellBackgroundStyle)backgroundStyleForIndexPath:(NSIndexPath*)indexPath tableView:(UITableView*)tableView {
    NSInteger maxRow = MAX(0, [tableView numberOfRowsInSection:indexPath.section] - 1); // catch the case of a section with 0 rows

    if (maxRow == 0) {
        return JSCellBackgroundStyleSolitary;

    } else if (indexPath.row == 0) {
        return JSCellBackgroundStyleTop;

    } else if (indexPath.row == maxRow) {
        return JSCellBackgroundStyleBottom;

    } else {
        return JSCellBackgroundStyleMiddle;
    }
}

- (UIView*)backgroundViewForStyle:(JSCellBackgroundStyle)style {
    NSMutableArray *stylePool = backgroundViewPool[style];

    // if we have a reusable view available, remove it from the pool and return it
    if ([stylePool count] > 0) {
        UIView *reusableView = stylePool[0];
        [stylePool removeObject:reusableView];

        return reusableView;

    // if we don't have any reusable views, make a new one and return it
    } else {
        UIView *newView = [[UIView alloc] init];

        NSLog(@"Created a new view for style %i", style);

        switch (style) {
            case JSCellBackgroundStyleTop:
                newView.backgroundColor = [UIColor blueColor];
                break;

            case JSCellBackgroundStyleMiddle:
                newView.backgroundColor = [UIColor greenColor];
                break;

            case JSCellBackgroundStyleBottom:
                newView.backgroundColor = [UIColor yellowColor];
                break;

            case JSCellBackgroundStyleSolitary:
                newView.backgroundColor = [UIColor redColor];
                break;
        }

        return newView;
    }
}

@end

, , , , .

+5

, , . , , - , - , !

, . :

  • . .
  • .
  • .

, , UITableView , , . , , /.

, / , .

, . , .

:

Screenshot showing a customized grouped table view

:

@implementation RootViewController
{
    NSMutableDictionary *_backgroundViews;
}

- (void)viewDidLoad
{
    _backgroundViews = [NSMutableDictionary dictionary];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 100;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return section / 10 + 1;
}

- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.backgroundView = nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    cell.textLabel.backgroundColor = [UIColor clearColor];
    if (!cell.backgroundView || ![cell.backgroundView isKindOfClass:[UIImageView class]]) {
        NSInteger section = [indexPath section];
        NSInteger row = [indexPath row];
        NSInteger maxRow = [tableView numberOfRowsInSection:section] - 1;
        NSString *imageName = nil;
        UIEdgeInsets insets = UIEdgeInsetsZero;
        if (maxRow == 0) {
            // single cell
            imageName = @"singlebackground";
            insets = UIEdgeInsetsMake(12, 12, 12, 12);
        } else if (row == 0) {
            // top cell
            imageName = @"topbackground";
            insets = UIEdgeInsetsMake(12, 12, 0, 12);
        } else if (row == maxRow) {
            // bottom cell
            imageName = @"bottombackground";
            insets = UIEdgeInsetsMake(0, 12, 12, 12);
        } else {
            // middle cell
            imageName = @"middlebackground";
            insets = UIEdgeInsetsMake(0, 12, 0, 12);
        }

        NSMutableSet *backgrounds = [_backgroundViews objectForKey:imageName];
        if (backgrounds == nil) {
            backgrounds = [NSMutableSet set];
            [_backgroundViews setObject:backgrounds forKey:imageName];
        }

        UIImageView *backgroundView = nil;
        for (UIImageView *candidate in backgrounds) {
            if (candidate.superview == nil) {
                backgroundView = candidate;
                break;
            }
        }
        if (backgroundView == nil) {
            backgroundView = [[UIImageView alloc] init];
            backgroundView.image = [[UIImage imageNamed:imageName] resizableImageWithCapInsets:insets];
            backgroundView.backgroundColor = [UIColor whiteColor];
            backgroundView.opaque = YES;
        }
        cell.backgroundView = backgroundView;
    }

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    [[cell textLabel] setText:[NSString stringWithFormat:@"[%d, %d]", [indexPath section], [indexPath row]]];

    return cell;
}

, , ( , , ):

  • singlebackground.png:

    singlebackground.png

  • topbackground.png:

    topbackground.png

  • middlebackground.png:

    middlebackground.png

  • bottombackground.png:

    bottombackground.png

+1

, , 3 . .

, backgroundViews . , , backgroundViews:

! ARC.

static NSString *identifierSingle = @"single";
static NSString *identifierTop = @"top";
static NSString *identifierBtm = @"btm";
static NSString *identifierMid = @"mid";


@implementation RootViewController {
    NSMutableDictionary *_backgroundViewPool;
}

- (id)initWithStyle:(UITableViewStyle)style
{
    if (self = [super initWithStyle:style]) {
        _backgroundViewPool = [[NSMutableDictionary alloc] init];

        UITableView *tableView = [self tableView];
        [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    }

    return self;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 6;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.

    if (section == 0) {
        return 1;
    }

    return 10;
}

- (NSString *)tableView:(UITableView *)tableView identifierForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger section = [indexPath section];
    NSInteger row = [indexPath row];
    NSInteger maxRow = [tableView numberOfRowsInSection:section] - 1;

    if (maxRow == 0) {
        // single cell
        return identifierSingle;
    } else if (row == 0) {
        // top cell
        return identifierTop;
    } else if (row == maxRow) {
        // bottom cell
        return identifierBtm;
    } else {
        // middle cell
        return identifierMid;
    }
}

- (UIColor *)tableView:(UITableView *)tableView colorForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger section = [indexPath section];
    NSInteger row = [indexPath row];
    NSInteger maxRow = [tableView numberOfRowsInSection:section] - 1;
    UIColor *color = nil;


    if (maxRow == 0) {
        // single cell
        color = [UIColor blueColor];
    } else if (row == 0) {
        // top cell
        color = [UIColor redColor];
    } else if (row == maxRow) {
        // bottom cell
        color = [UIColor greenColor];
    } else {
        // middle cell
        color = [UIColor yellowColor];
    }
    return color;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *colorIdentifier = [self tableView:tableView identifierForRowAtIndexPath:indexPath];
    NSString *CellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    [[cell textLabel] setText:[NSString stringWithFormat:@"[%d, %d]", [indexPath section], [indexPath row]]];
    [[cell textLabel] setBackgroundColor:[UIColor clearColor]];

    NSMutableSet *set = [self backgroundPoolForIdentifier:colorIdentifier];
    UIView *backgroundView = [set anyObject];;

    if (backgroundView) {
        [set removeObject:backgroundView];
    } else {
        backgroundView = [[UIView alloc] init];
        [backgroundView setBackgroundColor:[self tableView:tableView colorForRowAtIndexPath:indexPath]];
    }

    [cell setBackgroundView:backgroundView];

    return cell;
}

#pragma mark - Table view delegate

- (NSMutableSet *)backgroundPoolForIdentifier:(NSString *)identifier {
    NSMutableSet *set = [_backgroundViewPool valueForKey:identifier];
    if (!set) {
        set = [[NSMutableSet alloc] init];
        [_backgroundViewPool setValue:set forKey:identifier];
    }
    return set;
}

- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    [[self backgroundPoolForIdentifier:cell.reuseIdentifier] addObject:cell.backgroundView];
}

@end
0

EDIT -

, , . , . , UIImage UIImageView. , UIImageView " " , image UIImage indexPath.

, [UIImage imageNamed:@""] iOS . (, ), / .

tableView:cellForRowAtIndexPath:, , tableView .

- :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *singleCellID = @"single";
    static NSString *firstCellID = @"first";
    static NSString *middleCellID = @"middle";
    static NSString *lastCellID = @"last";

    NSString *cellID = nil;

    NSInteger section = [indexPath section];
    NSInteger row = [indexPath row];
    NSInteger maxRow = [tableView numberOfRowsInSection:section] - 1;
    UIColor *color = nil;

    if (maxRow == 0) {
        // single cell
        cellID = singleCellID;
    } else if (row == 0) {
        // top cell
        cellID = firstCellID;
    } else if (row == maxRow) {
        // bottom cell
        cellID = lastCellID;
    } else {
        // middle cell
        cellID = middleCellID;
   }

   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];

   if (cell == nil) {
       if (cellID == singleCellID) {
           // create single cell
           cell = ...
           cell.backgroundView = ...
       }
       else if (cellID == firstCellID) {
           // create first cell
           cell = ...
           cell.backgroundView = ...
       }
       else if (cellID == lastCellID) {
           // create last cell
           cell = ...
           cell.backgroundView = ...
       }
       else {
           // create middle cell
           cell = ...
           cell.backgroundView = ...
       }
   }
}
0

[EDIT] , , , .backgroundView tableView:cellForRowAtIndexPath: , - , backgroundColor ( ).

, , (, - .contentView) - /contentView.alpha 0.5, . - - , UITableView

- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell = nil;
    static NSString* identifer = @"Cell";
    cell = [tableView dequeueReusableCellWithIdentifier:identifer];
    if(cell==nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
        cell.backgroundView = [YourCustomView new];//assign your custom background view here
    }
    cell.textLabel.text = [NSString stringWithFormat:@"%d",indexPath.row];
    //update background view color based on index path row
    if(indexPath.row==0)
        cell.backgroundView.backgroundColor = [UIColor redColor];
    else if(indexPath.row==1)
        cell.backgroundView.backgroundColor = [UIColor yellowColor];
    else
        cell.backgroundView.backgroundColor = [UIColor blueColor];
    return cell;
}
0

, cellForRowAtIndexPath: . UITableView * willDisplayCell:(UITableViewCell*).

ie: 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
....
return cell;

// cell maybe nil

, UITableViewCell, , , , .

ie:

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

  static NSString *CellIdentifier = @"Cell";
  return [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath] 
        || [UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]
        ;
}

" 1 ", .

0

apple docs, , , , .

, , , , .

, , , BG - willDisplayCell:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
  NSInteger section = [indexPath section];
  NSInteger row = [indexPath row];
  NSInteger maxRow = [tableView numberOfRowsInSection:section] - 1;
  UIColor *color = nil;


  if (maxRow == 0) {
    // single cell
    color = [UIColor blueColor];
  } else if (row == 0) {
    // top cell
    color = [UIColor redColor];
  } else if (row == maxRow) {
    // bottom cell
    color = [UIColor greenColor];
  } else {
    // middle cell
    color = [UIColor yellowColor];
  }

  UIView *backgroundView = nil;

  //***This is the different part***//

  if (cell.backgroundView != nil) {
    NSLog(@"Old Cell, reuse BG View");
    backgroundView = cell.backgroundView;
  } else {
    NSLog(@"New Cell, Create New BG View");
    backgroundView = [[UIView alloc] init];
    [cell setBackgroundView:[backgroundView autorelease]];
  }

  [backgroundView setBackgroundColor:color];  
}

didEndDisplayingCell:.

0

, , , , , , , , , :

NSMutableArray *_viewArray;
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    #define kTotalNoOfRows 1000
        _viewArray = [[NSMutableArray alloc] initWithCapacity:kTotalNoOfRows];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   for (int i = 0; i < kTotalNoOfRows; i++) {
        UIView * backGroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];

        if (kTotalNoOfRows == 0)
            [backGroundView setBackgroundColor:[UIColor redColor]];
        else if (i == 0)
            [backGroundView setBackgroundColor:[UIColor greenColor]];
        else if (i == (kTotalNoOfRows - 1))
            [backGroundView setBackgroundColor:[UIColor blueColor]];
        else
            [backGroundView setBackgroundColor:[UIColor yellowColor]];

        [_viewArray addObject:backGroundView];        
    }

    return kTotalNoOfRows;
}


- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell = nil;
    static NSString* middleCell = @"middleCell";
    cell = [tableView dequeueReusableCellWithIdentifier:middleCell];
    if(cell==nil) {
        NSInteger maxRow = [tableView numberOfRowsInSection:indexPath.section] - 1;
        if (maxRow != 0 && indexPath.row != 0 && indexPath.row != maxRow) {
            middleCell = nil;
        }
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:middleCell];
        cell.backgroundView = [_viewArray objectAtIndex:indexPath.row];//assign your custom background view here
        [cell.textLabel setBackgroundColor:[UIColor clearColor]];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"%d",indexPath.row];
    return cell;
}

; , ,

  • Views:

    UIView * _topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    UIView * _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    UIView * _middleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    UIView * _lonelyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    [_topView setBackgroundColor:[UIColor redColor]];
    [_bottomView setBackgroundColor:[UIColor greenColor]];
    [_middleView setBackgroundColor:[UIColor blueColor]];
    [_lonelyView setBackgroundColor:[UIColor yellowColor]];
    _viewDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                     _topView, @"topView",
                                     _bottomView, @"bottomView",
                                     _middleView, @"middleView",
                                     _lonelyView, @"lonelyView", nil];
    
  • unarchiver

    - (UIView *) getBackgroundViewWith : (NSInteger) maxRow currentRow : (NSInteger) row{
        if (maxRow == 0) {
            return (UIView *)[NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:[_viewDictionary valueForKey:@"lonelyView"]]];//[[_viewDictionary valueForKey:@"lonelyView"] copy];
        } else if (row == 0) {
            return (UIView *)[NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:[_viewDictionary valueForKey:@"topView"]]];//[[_viewDictionary valueForKey:@"topView"] copy];
        } else if (row == maxRow) {
            return (UIView *)[NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:[_viewDictionary valueForKey:@"bottomView"]]];//[[_viewDictionary valueForKey:@"bottomView"] copy];
        } else {
            return (UIView *)[NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:[_viewDictionary valueForKey:@"middleView"]]];//[[_viewDictionary valueForKey:@"middleView"] copy];
        }
        return nil;
    }
    

, SIGBART. , .

0

, , , . , tableView: willDisplayCell: forRowAtIndexPath: . , . , , , .

@synchronized (anObject) {} , .

@synchronized (self) {
    UIView *backgroundView = nil;

    for (UIView *bg in _backgroundViewPool) {
        if (color == [bg backgroundColor]) {
            backgroundView = bg;
            break;
        }
    }

    if (backgroundView) {
        [backgroundView retain];
        [_backgroundViewPool removeObject:backgroundView];
    } else {
        backgroundView = [[UIView alloc] init];
        [backgroundView setBackgroundColor:color];
    }
}
0

All Articles