我遇到了问题,描述它的最佳方式是向您展示图片。 该部分的最后一行应该是一个光泽样式和文本对齐中心。 首先,我有一个表格,其中一个部分是这样的:
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;
我真的不明白这个问题。请帮我。谢谢! ;)
答案 0 :(得分:0)
嗯,屏幕截图显示的问题表明您对cellIsLastOne
的计算错误。
但即使它是正确的,当你滚动表格视图时会出现很多问题,因为正在重复使用单元格。要修复,您可以:
else
块基本上,当您致电dequeueReusableCellWithIdentifier:
时,它可能会返回您之前添加了光泽图层的单元格。此外,这可以多次添加光泽层。
答案 1 :(得分:0)
我编辑可能对您有帮助的代码。
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;
此代码可以有效地重用单元格并节省内存。