gpt4 book ai didi

objective-c - Xcode 使 View 在单击时出现过渡

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:06:43 25 4
gpt4 key购买 nike

如果这看起来像是一个基本问题,我很抱歉,但我的谷歌搜索技能似乎无法找到我的答案,我希望你能帮忙!

基本上,我希望我的应用程序有一个按钮或一个标签,然后单击它会在其下方显示一些文本(以常见问题解答的形式,如此处的链接:http://aptow.com/faq)并用它向下推所有内容.我知道如何单击并显示以前隐藏的元素,但我不确定如何实现向下滑动效果,同时在单击时将其他所有内容进一步推到屏幕下方,然后重新 -一旦用户再次单击以重新隐藏之前隐藏的内容,将其安排在屏幕顶部附近。我提供的链接准确地显示了我想要做什么。

有什么想法吗?提前致谢。

最佳答案

如果您想执行类似于您发布链接的常见问题解答页面的操作,您可以使用使用动态单元格高度的 UITableView。

一个简单的 UITableViewController 看起来像这样:

#import "ViewController.h"

@interface ViewController () {
NSArray *data;
int selected;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
selected = -1;

data = [NSArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:@"Title 1", @"title", @"Lorem ipsum dolor bla blab bla", @"details", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Title 2", @"title", @"Lorem ipsum dolor bla blab bla", @"details", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Title 3", @"title", @"Lorem ipsum dolor bla blab bla", @"details", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Title 4", @"title", @"Lorem ipsum dolor bla blab bla", @"details", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Title 5", @"title", @"Lorem ipsum dolor bla blab bla", @"details", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Title 6", @"title", @"Lorem ipsum dolor bla blab bla", @"details", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Title 7", @"title", @"Lorem ipsum dolor bla blab bla", @"details", nil],nil ];

}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == selected) {
return 130;
} else
return tableView.rowHeight;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [data count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"ci";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
NSDictionary *tmp = [data objectAtIndex:indexPath.row];

cell.textLabel.text = nil;


static int TITLE_LABEL_TAG = 122;
UILabel *titleLabel = (UILabel *)[cell.contentView viewWithTag:TITLE_LABEL_TAG];
if (!titleLabel) {
titleLabel = [[UILabel alloc] initWithFrame:CGRectNull];
titleLabel.tag = TITLE_LABEL_TAG;
[cell.contentView addSubview:titleLabel];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.numberOfLines = 1;
titleLabel.font = [UIFont boldSystemFontOfSize:16];
[cell.contentView addSubview:titleLabel];
}
titleLabel.frame = (CGRectMake(8, 8, cell.contentView.frame.size.width-16, 20));
titleLabel.text = [tmp objectForKey:@"title"];

static int DETAIL_LABEL_TAG = 123;
UILabel *detailLabel = (UILabel *)[cell.contentView viewWithTag:DETAIL_LABEL_TAG];
if (!detailLabel) {
detailLabel = [[UILabel alloc] initWithFrame:CGRectNull];
detailLabel.tag = DETAIL_LABEL_TAG;
[cell.contentView addSubview:detailLabel];
detailLabel.backgroundColor = [UIColor clearColor];
detailLabel.numberOfLines = 0;
}
if (indexPath.row == selected) {
detailLabel.frame = (CGRectMake(8, 30, cell.contentView.frame.size.width-16, 100));

detailLabel.hidden = NO;
detailLabel.text = [tmp objectForKey:@"details"];
} else {
detailLabel.hidden = YES;
}

return cell;
}

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

NSMutableArray *ip2reload = [NSMutableArray array];
if (selected > -1 && selected != indexPath.row)
[ip2reload addObject:[NSIndexPath indexPathForRow:selected inSection:0]];
[ip2reload addObject:[NSIndexPath indexPathForRow:indexPath.row inSection:0]];
selected = indexPath.row;
[tableView reloadRowsAtIndexPaths:ip2reload withRowAnimation:UITableViewRowAnimationAutomatic];
}


@end

您肯定需要根据内容计算所选行的实际行高(例如,如此处所述:Iphone - when to calculate heightForRowAtIndexPath for a tableview when each cell height is dynamic?)。

并且您可以使用 scrollToRowAtIndexPath:atScrollPosition:animated 来确保所选单元格位于屏幕顶部。

关于objective-c - Xcode 使 View 在单击时出现过渡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12068918/

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