gpt4 book ai didi

ios - 试图了解 cellForRowAtIndexPath 和 heightForRowAtIndexPath 在处理 UITableViewCells 时如何工作

转载 作者:行者123 更新时间:2023-11-28 18:40:20 29 4
gpt4 key购买 nike

在StackOverflow好心人的帮助下,我有这两个方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault)
reuseIdentifier:@"business"];

NSString *comment = [[items_array objectAtIndex:[indexPath row]] objectForKey:(@"comment")];

NSLog(businessPrivacy);
// FIND IF THE BUSINESS PLAN IS PRIVATE OR NOT.

CGSize constraint = CGSizeMake(320 - (10 * 2), 20000.0f);

CGSize size = [comment sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, MAX(size.height, 44.0f) + 20.0f)];
label.numberOfLines = 0;
label.lineBreakMode = UILineBreakModeWordWrap;
label.text = comment;

[cell.contentView addSubview:label];

return cell;
}

//Cell size for word wrapping.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSString *comment = [[items_array objectAtIndex:[indexPath row]] objectForKey:(@"comment")];

CGSize constraint = CGSizeMake(320 - (10 * 2), 20000.0f);

CGSize size = [comment sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

CGFloat height = MAX(size.height, 44.0f);

return height + (10 * 2);
}

它们的效果是从默认字体大小减小字体大小,并动态调整大小以几乎适合评论的整个文本,无论该文本可能有多长。

令我感到困惑的是,我在这两种方法中都有一些相同的代码,而且我不知道它到底应该是什么样子。我确实知道这两种方法都被调用了,但不确定应该在哪里调用它们才能正常工作。

提前感谢您的建议。

最佳答案

看起来您两者都需要它们。我已经用注释重写了您的代码,以便您可以理解它:

//This function is used to create the cell and the content of the cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Here you are allocating a new cell with a reuse identifier. This is only needed if
//you plan on reusing the cells and using [tableView dequeueReusableCellWithIdentifier:cellIdentifier]
//Reusable cells will save you some memory and make your tableview work more smoothly,
//however here you are not selecting from the reusable pool, and are instead making a new cell each time
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault)
reuseIdentifier:@"business"];

//This is pulling the text for the comment, it is obviously necessary
NSString *comment = [[items_array objectAtIndex:[indexPath row]] objectForKey:(@"comment")];

NSLog(businessPrivacy);
// FIND IF THE BUSINESS PLAN IS PRIVATE OR NOT.

// This is creating a constraint size for the label you are making
CGSize constraint = CGSizeMake(320 - (10 * 2), 20000.0f);

// This is determining the size that you will need for the label based on the comment
// length and the contraint size
CGSize size = [comment sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

// Here you are creating your label and initializing it with the frame that you have
// determined by your size calculations above
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, MAX(size.height, 44.0f) + 20.0f)];

// setting up the label properties
label.numberOfLines = 0;
label.lineBreakMode = UILineBreakModeWordWrap;
label.text = comment;

// adding the label view to the cell that you have created
[cell.contentView addSubview:label];

// return the cell for the table view
return cell;
}

//Cell size for word wrapping.
//This method will determine how tall each row needs to be.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
//Here you are getting the comment again. This is necessary to determine how tall you need
//the cell to be
NSString *comment = [[items_array objectAtIndex:[indexPath row]] objectForKey:(@"comment")];

// Again you are getting the constraints because you are going to us this size
// to constrain the height of the cell just like you determined the size for the label.
CGSize constraint = CGSizeMake(320 - (10 * 2), 20000.0f);

// Again, determining the size that we will need for the label, because it will drive
// the height of the cell
CGSize size = [comment sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

// Finally, we can determine the height for the cell!
CGFloat height = MAX(size.height, 44.0f);

// return the height, which is the height of the label plus some extra space to make
// it look good.
return height + (10 * 2);
}

基本上,cellForRow... 函数创建单元格的内容,heightForRow... 函数确定高度。您需要它们中的所有信息,因为 cell 函数仅创建单元格的内容,而 height 函数创建行的高度和注释、约束和大小对于确定单元格内容的 label 大小和单元格高度都很重要。

关于ios - 试图了解 cellForRowAtIndexPath 和 heightForRowAtIndexPath 在处理 UITableViewCells 时如何工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11852963/

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