gpt4 book ai didi

ios - 如何在 ios 中圆角 Jade 米标题查看所有角边及其可扩展单元格

转载 作者:行者123 更新时间:2023-11-30 14:14:53 24 4
gpt4 key购买 nike

嗨,我是 Objective-C 的初学者,我正在项目中做可扩展列表,一切都即将到来,但我想将 Headerview 所有角边及其可扩展单元格圆角,但根据我的代码,当我们扩展单元格时然后单元格就像第一个图像一样出现,但我想在我们像第二个图像一样展开时显示单元格,请帮助我,这是我的代码:-

#import "ViewController.h"

@interface ViewController ()
{
UITableView * expandableTableView;
}

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
[self initialization];
}

-(void)initialization
{
expandableTableView = [[UITableView alloc]init];
expandableTableView.frame = CGRectMake(5,15,310,self.view.frame.size.height - 50);
expandableTableView.dataSource=self;
expandableTableView.delegate=self;
expandableTableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[expandableTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];

[expandableTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];

expandableTableView.backgroundColor = [UIColor whiteColor];

UIEdgeInsets inset = UIEdgeInsetsMake(5, 0, 0, 0);
expandableTableView.contentInset = inset;

[self.view addSubview:expandableTableView];


arrayForBool=[[NSMutableArray alloc]init];
sectionTitleArray=[[NSArray alloc]initWithObjects:
@"India",
@"Usa",
@"Uae",
@"Japan",
@"SouthAfrica",
@"Jarmany",
@"England",
@"Rasya",
nil];

for (int i=0; i<[sectionTitleArray count]; i++) {
[arrayForBool addObject:[NSNumber numberWithBool:NO]];
}
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

if ([[arrayForBool objectAtIndex:section] boolValue]) {
return 1;
}
else

return 0;
}

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

static NSString *cellid=@"hello";

UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellid];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellid];
}

BOOL manyCells = [[arrayForBool objectAtIndex:indexPath.section] boolValue];

if(!manyCells)
{
cell.backgroundColor=[UIColor clearColor];
cell.textLabel.text=@"";
}

else
{
cell.textLabel.text=[NSString stringWithFormat:@"%@ %d",[sectionTitleArray objectAtIndex:indexPath.section],indexPath.row+1];
cell.textLabel.font=[UIFont systemFontOfSize:15.0f];
cell.backgroundColor=[UIColor lightGrayColor];
cell.imageView.image=[UIImage imageNamed:@"point.png"];
}
cell.textLabel.textColor=[UIColor blackColor];

[cell.layer setCornerRadius:7.0f];
[cell.layer setMasksToBounds:YES];

return cell;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [sectionTitleArray count];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

[arrayForBool replaceObjectAtIndex:indexPath.section withObject:[NSNumber numberWithBool:NO]];

[expandableTableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([[arrayForBool objectAtIndex:indexPath.section] boolValue]) {
return 100;
}
return 0;
}


- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView *footerView;

footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 10)];
footerView.backgroundColor = [UIColor whiteColor];
return footerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 40;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

UIView *sectionView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 280,40)];
sectionView.tag=section;
sectionView.backgroundColor = [UIColor lightGrayColor];
UILabel *viewLabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 0, expandableTableView.frame.size.width-10, 40)];
viewLabel.backgroundColor=[UIColor clearColor];
viewLabel.textColor=[UIColor blackColor];
viewLabel.font=[UIFont systemFontOfSize:15];
viewLabel.text=[NSString stringWithFormat:@"List of %@",[sectionTitleArray objectAtIndex:section]];
[sectionView addSubview:viewLabel];
sectionView.layer.cornerRadius = 5.0;
sectionView.clipsToBounds = YES;

UITapGestureRecognizer *headerTapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionHeaderTapped:)];
[sectionView addGestureRecognizer:headerTapped];

return sectionView;
}

- (void)sectionHeaderTapped:(UITapGestureRecognizer *)gestureRecognizer{

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:gestureRecognizer.view.tag];
if (indexPath.row == 0) {
BOOL collapsed = [[arrayForBool objectAtIndex:indexPath.section] boolValue];
for (int i=0; i<[sectionTitleArray count]; i++) {
if (indexPath.section==i) {
[arrayForBool replaceObjectAtIndex:i withObject:[NSNumber numberWithBool:!collapsed]];
}
}
[expandableTableView reloadSections:[NSIndexSet indexSetWithIndex:gestureRecognizer.view.tag] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
[cell setPreservesSuperviewLayoutMargins:NO];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end

enter image description here

enter image description here

最佳答案

为了按照您希望的方式渲染标题和单元格,您需要使用图层蒙版来圆角,而不是使用 cornerRadius 帮助器。该掩码需要根据该部分的展开/折叠状态进行更改。

幸运的是,您似乎已经在跟踪 arrayForBool 变量中展开/折叠的部分。附带说明一下,由于缺乏清晰度,这是一个糟糕的名称选择,expandedSections 可能更合适。

对于折叠部分,图层蒙版需要将标题 View 的所有四个角变圆。

对于展开的部分,标题 View 的图层蒙版需要将顶部两个角变圆,而单元格 View 的图层蒙版需要将底部两个角变圆。

您可以使用UIBezierPath +bezierPathWithRoundedRect:byRoundingCorners:cornerRadii:使图层的某些角变圆。

关于ios - 如何在 ios 中圆角 Jade 米标题查看所有角边及其可扩展单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31315545/

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