gpt4 book ai didi

ios - 设置 AccessoryType 破坏 UITableViewCell

转载 作者:行者123 更新时间:2023-11-28 22:27:20 25 4
gpt4 key购买 nike

我使用自定义表格单元格,标签和图像与标签相连。

如果我添加

[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]

它破坏了我的表格 View 。图像破坏和背景颜色。

区别如下:

enter image description here enter image description here

有人知道问题出在哪里吗?谢谢

编辑:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"TableCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

if ([self labelCellNib]) {
[[self labelCellNib] instantiateWithOwner:self options:nil];
} else {
[[NSBundle mainBundle] loadNibNamed:@"LabelCell" owner:self options:nil];
}

cell = self.labelCell;
self.labelCell = nil;
}

static NSDateFormatter *dateFormatter = nil;
if (dateFormatter == nil)
dateFormatter = [[NSDateFormatter alloc] init];

static NSCalendar *calendar;
if(calendar == nil)
calendar= [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSString * timeStampString = [sqlTime objectAtIndex:indexPath.row];
NSTimeInterval _interval=[timeStampString doubleValue];
NSDate *createdAt = [NSDate dateWithTimeIntervalSince1970:_interval];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];

// Datum in die Tabellen-Zelle einfügen
UILabel *label4 = (UILabel *)[cell viewWithTag:LABEL4];
label4.text = [NSString stringWithFormat:@"%@", [dateFormatter stringFromDate:createdAt]];

// Configure the cell...
UILabel *label1 = (UILabel *)[cell viewWithTag:LABEL1];
label1.text = [NSString stringWithFormat:@"%@ %@", [sqlData objectAtIndex:indexPath.row], sonderzeichen];

UILabel *label2 = (UILabel *)[cell viewWithTag:LABEL2];
label2.text = [NSString stringWithFormat:@"Preis pro %@: %@ €", sonderzeichen, [sqlPreis objectAtIndex:indexPath.row]];

UILabel *label3 = (UILabel *)[cell viewWithTag:LABEL3];
UIImageView *image = (UIImageView *)[cell viewWithTag:IMAGE_TAG];

if (indexPath.row==[itemsArray count]-1) {
image.image = [UIImage imageNamed:@"default.png"];
label3.text = @"-";
} else if ([itemsArray count]>1) {
int data_old = [[NSString stringWithFormat:@"%@", [sqlData objectAtIndex:indexPath.row]] intValue];
int data_new = [[NSString stringWithFormat:@"%@", [sqlData objectAtIndex:indexPath.row+1]] intValue];
label3.text = [NSString stringWithFormat:@"%i", data_old-data_new];

NSLog(@"%d -- %d", data_old, data_new);
if (data_new>data_old) {
image.image = [UIImage imageNamed:@"pfeil_runter.png"];
} else if (data_new<data_old) {
image.image = [UIImage imageNamed:@"pfeil_hoch.png"];
} else {
image.image = [UIImage imageNamed:@"minus.png"];
}
}

[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];

cell.contentView.backgroundColor = indexPath.row % 2? [UIColor colorWithRed:228.0/255.0 green:244.0/255.0 blue:199.0/255.0 alpha:1]:[UIColor whiteColor];
return cell;
}

最佳答案

您需要设置单元格的背景颜色,而不是 contentView,但这在 cellForRowAtIndexPath 中不起作用。它需要在 willDisplayCell:forRowAtIndexPath:

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = indexPath.row % 2? [UIColor colorWithRed:228.0/255.0 green:244.0/255.0 blue:199.0/255.0 alpha:1]:[UIColor whiteColor];
}

就图像被压扁而言,这与 subview 的大小及其约束有关。我认为您可以通过为两个标签(上下一对)提供宽度限制来解决这些问题,但使它们的优先级 <1000,因此当添加附件 View 时,这些标签的宽度会变小。同时给左边的 ImageView 一个固定的宽度。

关于ios - 设置 AccessoryType 破坏 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18549363/

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