gpt4 book ai didi

ios - iOS 应用程序 : XIB or coding? 的自定义单元格。高度和框架位置的问题

转载 作者:搜寻专家 更新时间:2023-10-30 20:18:55 25 4
gpt4 key购买 nike

我正在尝试构建一个包含多个 subview 的自定义 UITableCellView,但现在我正在努力处理其中两个(最大的):一个 UIImageView(我不介意使用 UIWebView,因为图像来自互联网)和一个 UITextView,它应该在不滚动的情况下立即显示所有文本。在这种情况下,我开始使用 XIB 文件和子类化 UITableCellView(自动布局禁用)构建自定义单元格:

enter image description here

我向自定义类添加了两个新属性 (IBOutlet)(非常简单):

@interface NewsFeedPostCell : UITableViewCell<UITextViewDelegate>
{
UIImageView *picView;
UITextView *postContent;
}

@property IBOutlet UIImageView *picView;
@property IBOutlet UITextView *postContent;

@end

之后,我使用它们来填充数据(我从 iTunes 获取了一些返回漂亮的 JSON @"http://itunes.apple.com/search?term=ice%20age&country=us&entity=movie "的东西)。目前我专注于正确显示数据而不获取每个单元格的确切大小(除了不同文本的大小)但我有很多问题:内容未正确显示,因为文本未全部显示,首先行不显示图像等。我在 stackoverflow 上读了很多书,但我开始对人们解决它的许多不同方法感到痛苦,因为我也只使用代码进行了测试,但我遇到了类似甚至更糟的事情。

我在这里的主要问题/提示是​​什么可能是构建具有不同高度和 subview 的自定义单元格的复杂表格的最佳方法,以及如何计算大小,或者比这更好,何时何地(在代码中)因为heightForRowAtIndexPath 在我下载图像以获取它们的大小之前被调用。有很多用于布局、重绘和“适合大小”的属性,但其中任何一个似乎都没有像我想象的那样工作。我觉得我太了解 iOS 应用程序中的布局是如何工作的,而且在我看来 UI 元素根本没有灵 active :(

这是使用的源代码(顺便说一句,我使用 AFNetworking 库来获取图像并将它们加载到 UIImageView 中):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"PostCell";
NewsFeedPostCell *cell;

NSDictionary *post = [self.posts objectAtIndex:indexPath.row];
NSString *postText = [post objectForKey:@"longDescription"];

cell= (NewsFeedPostCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"NewsFeedCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}

CGRect picFrame = cell.picView.frame;
picFrame = CGRectMake(100, 0, 300, 100);
[cell.picView setFrame:picFrame];
[cell.picView setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[post objectForKey:@"artworkUrl100"]]]
placeholderImage:nil
success:^(NSURLRequest *request , NSHTTPURLResponse *response , UIImage *image ){
NSLog(@"Loaded successfully: %d", [response statusCode]);
[cell.picView setImage:image];
[cell.picView sizeToFit];

}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){
NSLog(@"failed loading: %@", error);
}
];

CGSize stringSize = [self calculateTextHeight:postText];

CGRect postFrame = cell.postContent.frame;
postFrame = CGRectMake(10, 100, 300, stringSize.height);
cell.postContent.frame = postFrame;

cell.postContent.autoresizingMask = UIViewAutoresizingFlexibleHeight;

cell.postContent.font = [UIFont systemFontOfSize:POST_CONTENT_FONT_SIZE];
cell.postContent.text= postText;

[cell.postContent setContentInset:UIEdgeInsetsZero];

[cell.postContent sizeThatFits:cell.postContent.contentSize];


return cell;
}

查看屏幕截图中的问题(我正在使用 iOS 模拟器模拟 3.5"视网膜设备并用作构建应用程序 iOS SDK 6.1 的目标)

第一行(图片在顶栏下方;您必须滚动才能看到) This is first row

另一行没有正确显示文本,我必须向下/向上滚动才能得到它: enter image description here

enter image description here

并且不可能完成最后一行文本: enter image description here

最佳答案

制作具有不同高度的自定义单元格的最佳方法是:

为单元格创建一个 NSMutableArray:

@property (nonatomic,retain)NSMutableArray* cellsArray;

然后在 m.:

调用的第一个方法是

-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

在这里制作你的手机:

        -(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (!self.cellsArray) {

self.cellsArray = [[NSMutableArray alloc]init];

}

ListCell* cell = [[ListCell alloc] initWithFrame:CGRectMake(0, 0,, 50)];


// IMPLEMENT your custom cell here, with custom data, with custom,different height:

// after add this cell to your array:
[self.cellsArray addObject:cell];

// IMPORTANT STEP: set your cell height for the new height:

cell.frame = CGRectMake(cell.frame.origin.x, cell.frame.origin.y, 303, yourLastItemInCell.frame.origin.y + yourLastItemInCell.frame.size.height);


return cell.frame.size.height;
}

在你得到不同高度的自定义单元格之后:

#pragma mark - conform to delegates

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}

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

return 10;
}

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


return self.cellsArray[indexPath.row];

}

希望对你有帮助!

关于ios - iOS 应用程序 : XIB or coding? 的自定义单元格。高度和框架位置的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18700061/

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