gpt4 book ai didi

ios - 使用缓存的 UIView 设置 tableView 中的单元格背景 View :willDisplayCell:forRowAtIndexPath:

转载 作者:可可西里 更新时间:2023-11-01 04:35:49 24 4
gpt4 key购买 nike

这是我设置自定义分组表格 View 单元格背景的解决方案:

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

显然它没有像预期的那样工作,这让我来到这里,但是当我使用缓存 View 时它确实有效:

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

更新: 在 Jonathan 指出我不能对多个单元格使用相同的 View 之后,我决定遵循 TableView 模型,它有一个可重复使用的细胞队列。对于我的实现,我有一个可重复使用的背景 View 队列 (_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]];
}

除非您滚动得非常快,否则它会起作用。一些背景 View 消失了!我怀疑背景 View 仍在多个单元格中使用,但我真的不知道发生了什么,因为背景 View 一旦被重用就应该从队列中删除,因此无法使用背景 View 在多个可见单元格中。


自发布此问题以来,我一直在研究这个问题。当前在线分组表格 View 单元格的自定义背景 View 的解决方案并不令人满意,它们没有使用缓存 View 。此外,我不想使用 XJones 和 jszumski 提出的解决方案,因为一旦考虑到可重用的自定义单元格(例如,文本字段单元格、切换单元格、 slider 单元格),它就会变得很复杂。

最佳答案

您是否考虑过为“孤独”、“顶部”、“底部”和“中间”情况使用 4 个单独的单元格标识符,并在初始化单元格时仅设置一次 backgroundView?这样做方式让您可以利用 UITableView 自己的缓存和重用,而无需在其之上编写实现。


更新:分组的 UITableViewController 子类的实现,它使用最少数量的单元格重用标识符重用背景 View (Espresso 的用例)。 tableView:willDisplayCell:forRowAtIndexPath:tableView:didDisplayCell:forRowAtIndexPath: 负责应用或回收每个背景 View ,池化逻辑在 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

虽然您可以很容易地将所有 View 转储到一个重用池中,但它会使一些循环逻辑复杂化,而且这种方式更容易理解。

关于ios - 使用缓存的 UIView 设置 tableView 中的单元格背景 View :willDisplayCell:forRowAtIndexPath:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15990504/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com