gpt4 book ai didi

objective-c - 为什么我们在 UITableViewController 中检查 if (cell == nil)?

转载 作者:可可西里 更新时间:2023-11-01 03:34:41 26 4
gpt4 key购买 nike

我正在尝试实现基于 UITableView 的应用程序。为此,我选择 UITableViewStyle 是 Group。在我的 TableView 中,它们是 15 部分,每个部分有 1 行。

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

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

return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section==12)
{
return 120;
}
else
{
return 60;
}

}

我想在 12

部分添加一个 UITextView

为此我做了以下代码

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];



}
if ([indexPath section] == 12)
{
if([indexPath row]==0)
{
descriptionTextField=[[UITextView alloc] initWithFrame:CGRectMake(5, 8, 290, 106)];
descriptionTextField.font = [UIFont systemFontOfSize:15.0];
descriptionTextField.backgroundColor=[UIColor scrollViewTexturedBackgroundColor];

[descriptionTextField setDelegate:self];
[descriptionTextField setTag:2];
[descriptionTextField setText:@"Enter Location Description."];

descriptionTextField.keyboardType=UIKeyboardTypeDefault;
descriptionTextField.returnKeyType=UIReturnKeyNext;


descriptionTextField.textColor=[UIColor blackColor];
descriptionTextField.editable=YES;
descriptionTextField.autocapitalizationType=UITextAutocapitalizationTypeWords;
descriptionTextField.autocorrectionType=UITextAutocorrectionTypeDefault;
descriptionTextField.textAlignment=UITextAlignmentLeft;

UIToolbar* keboardToolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 32)];

UIBarButtonItem *extra=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *Done=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(keyboardDoneButtonActin:)];
[Done setWidth:65.0f];
[keboardToolBar setItems:[[[NSArray alloc]initWithObjects:extra,Done, nil]autorelease] ];
[extra release];
[Done release];

[keboardToolBar setTintColor:[UIColor blackColor]];
[keboardToolBar setAlpha:.70];
[descriptionTextField setInputAccessoryView:keboardToolBar];
[descriptionTextField setTag:101];
[cell.contentView addSubview:descriptionTextField];
[descriptionTextField release];
}
}

return cell;
}

在初始阶段, TableView 是这样的

enter image description here

如果我向上和向下滚动 tableview,则 uitextView 部分发生变化,它将显示多个位置。

enter image description here

我不明白我的错,为什么会这样?

如果我在 if(cell==nil) 中实现上面的代码

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

if ([indexPath section] == 12)
{
if([indexPath row]==0)
{
**/* implemention all code here*/**

[cell.contentView addSubview:descriptionTextField];
[descriptionTextField release];
}
}

return cell;

UITextView 不显示,我认为它没有分配。

那么代码实现的区别是什么如果(细胞==零){里面

如果(单元格 == 无){}外面

最佳答案

测试 if (cell == nil) 处理没有可重用单元格要出列的情况,在这种情况下,您必须创建一个新单元格。创建新单元格时,您负责构建其 View 层次结构。

关于objective-c - 为什么我们在 UITableViewController 中检查 if (cell == nil)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9701259/

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