gpt4 book ai didi

iphone - 滚动之前加载 UITableView 中的所有单元格

转载 作者:行者123 更新时间:2023-12-03 18:36:05 25 4
gpt4 key购买 nike

我有一个 UITableView,总共有 8 个单元格(部分),其中 6 个包含文本字段作为 subview ,其中 2 个包含按钮,另一个包含 TextView 。现在我在这里遇到了问题,因为我们都是意识到,当 View 仅出现时,可见单元格就会加载。因此,当我的应用程序加载时,我可以查看 5 个单元格,前 4 个包含文本字段,第 5 个单元格包含按钮。我可以看到数组计数为 4当我通过登录控制台显示它时,即

there are 4 objects in the array

现在滚动表格 View 后,我可以观察到以下内容:

there are 6 objects in the array

所以,发生的情况是,如果我要保存在 TableView 中输入的数据,或者说编辑以更改现有数据,我将被迫滚动 TableView 并在最后一个单元格中输入值。这样用户的情况不可能总是如此,因为我们不能期望他/她滚动表格 View 并输入完整的表单/表格 View 单元格条目。他/她所做的任何更改都会发生,只需单击完成/保存即可这是..!

此外,我有一个选择器 View 作为包含文本字段作为 subview 的最后一个单元格的输入 View 。因此,在我的选择器 View 中确实选择了行方法,我面临着包含选择器的最后一个单元格/部分(textField 作为 subview )的崩溃问题查看这里是我的方法的代码片段:

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{

switch (tagValues)
{

case 105:
{
self.textField = [self.fields objectAtIndex:3];
self.textField.text = [self.reminder objectAtIndex:row];
}
break;

case 107:
{
//Crash occurring in this case,sometimes its working wonder why
self.textField = [self.fields objectAtIndex:5];
self.textField.text = [self.group objectAtIndex:row];
}
break;

}

}

**Note**:I am using the same textField for adding as subview to all 6 cells,but each one with unique tags and own purpose,i.e. one for editing text using key board,one with date picker,one with general picker etc...

我想知道为什么有时从选择器 View (最后一节单元格)选择值后有时会崩溃,有时工作正常。有时它会在 case 107 中注释的上述行处崩溃,有时会崩溃在主自动释放池中。某些时间线程如下所示:

enter image description here

有什么方法可以让我们一次加载所有单元格,以便在滚动之前将所有文本字段添加到数组中。我相信不会有任何问题

更详细的编辑代码以供理解:

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

//NSString *identifier = @"UITableViewCell";

NSString *cellIdentifierF = nil;

static NSString *firstCellIdentifier = @"FirstCell";
static NSString *secondCellIdentifier = @"SecondCell";
static NSString *thirdCellIdentifier = @"ThirdCell";
static NSString *fourthCellIdentifier = @"FourthCell";
static NSString *fifthCellIdentifier = @"FifthCell";
static NSString *sixthCellIdentifier = @"SixthCell";
static NSString *seventhCellIdentifier = @"SeventhCell";
static NSString *eightCellIdentifier = @"EightCell";

if(indexPath.section == 0 && indexPath.row == 0)
{
cellIdentifierF = firstCellIdentifier;
}
else if(indexPath.section == 1 && indexPath.row == 0)
{
cellIdentifierF = secondCellIdentifier;
}
else if(indexPath.section == 2 && indexPath.row == 0)
{
cellIdentifierF = thirdCellIdentifier;
}
else if(indexPath.section == 3 && indexPath.row == 0)
{
cellIdentifierF = fourthCellIdentifier;
}
else if(indexPath.section == 4 && indexPath.row == 0)
{
cellIdentifierF = fifthCellIdentifier;
}
else if(indexPath.section == 5 && indexPath.row == 0)
{
cellIdentifierF = sixthCellIdentifier;
}
else if(indexPath.section == 6 && indexPath.row == 0)
{
cellIdentifierF = seventhCellIdentifier;
}
else if(indexPath.section == 7 && indexPath.row == 0)
{
cellIdentifierF = eightCellIdentifier;
}

UITableViewCell *cell = (UITableViewCell *)[atableView dequeueReusableCellWithIdentifier:cellIdentifierF];

atableView.backgroundColor = [UIColor clearColor];

textField = [[[UITextField alloc]initWithFrame:CGRectMake(15, 12, 300, 24)]autorelease];
textField.textColor = [UIColor whiteColor];
textField.delegate = self;
tagValues = textField.tag;

switch (indexPath.section)
{
case 0:
{
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifierF] autorelease];
cell.backgroundColor = [[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"buttonbg-1.png"]];

//Only add content to cell if it is new
if([cellIdentifierF isEqualToString: firstCellIdentifier])
{
textField.placeholder = @"Enter name";
[self.textField setValue:[UIColor purpleColor]
forKeyPath:@"_placeholderLabel.textColor"];
textField.tag = 101;
textField.text = reminderInstance.Name;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
NSLog(@"Value = %@",textField.text);
[cell.contentView addSubview:textField];
[self.fields addObject:textField];
}
}
}
break;

case 1:
{
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifierF] autorelease];
cell.backgroundColor = [[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"buttonbg-1.png"]];

//Only add content to cell if it is new
if([cellIdentifierF isEqualToString: secondCellIdentifier])
{
textField.tag = 102;
textField.text = reminderInstance.Event;
NSLog(@"Value = %@",textField.text);
[cell.contentView addSubview:textField];
[self.fields addObject:textField];
}
}
}
break;

case 2:
{
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifierF] autorelease];
cell.backgroundColor = [[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"buttonbg-1.png"]];

//Only add content to cell if it is new
if([cellIdentifierF isEqualToString: thirdCellIdentifier])
{
textField.placeholder = @"Click here to set date and time";
[self.textField setValue:[UIColor purpleColor]
forKeyPath:@"_placeholderLabel.textColor"];
textField.inputView = self.datePicker;
textField.text = reminderInstance.Date;
textField.tag = 103;
NSLog(@"Value = %@",textField.text);
[cell.contentView addSubview:textField];
[self.fields addObject:textField];
}
}
}
break;

case 3:
{
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifierF] autorelease];
cell.backgroundColor = [[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"buttonbg-1.png"]];

//Only add content to cell if it is new
if([cellIdentifierF isEqualToString: fourthCellIdentifier])
{
textField.tag = 105;
textField.text = reminderInstance.numDays;
textField.inputView = self.reminderPicker;
NSLog(@"Value = %@",textField.text);
[cell.contentView addSubview:textField];
[self.fields addObject:textField];
}
}
}
break;

case 4:
{
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifierF] autorelease];
cell.backgroundColor = [[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"buttonbg-1.png"]];

//Only add content to cell if it is new
if([cellIdentifierF isEqualToString: fifthCellIdentifier])
{
checkboxButton = [[[UIButton alloc] initWithFrame:CGRectMake(16,1,120, 44)]autorelease];
[checkboxButton setImage:[UIImage imageNamed:@"ewee.png"] forState:UIControlStateNormal];
[checkboxButton addTarget:self action:@selector(toggleButton:) forControlEvents:UIControlEventTouchUpInside];

NSString *one = reminderInstance.selString;
NSNumber* i = [NSNumber numberWithInt:[one intValue]];
BOOL isOn = [i boolValue];

if(isOn)
{
[checkboxButton setImage:[UIImage imageNamed:@"checkarrow.png"] forState:UIControlStateNormal];
}
else
{
[checkboxButton setImage:[UIImage imageNamed:@"ewee.png"] forState:UIControlStateNormal];
}
NSLog(@"String Val = %@",one);

[checkboxButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
[checkboxButton setImageEdgeInsets:UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0)];
[cell addSubview:checkboxButton];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(55, 10, 225, 24)];
label.text = @"Every Year";
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor clearColor];
[cell addSubview:label];
cell.textLabel.textColor = [UIColor whiteColor];
[label release];
}
}

}
break;

case 5:
{
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifierF] autorelease];
cell.backgroundColor = [[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"buttonbg-1.png"]];

//Only add content to cell if it is new
if([cellIdentifierF isEqualToString: sixthCellIdentifier])
{
textField.placeholder = @"Enter the number here";
[self.textField setValue:[UIColor purpleColor]
forKeyPath:@"_placeholderLabel.textColor"];
textField.text = num;
textField.text = reminderInstance.number;
textField.tag = 106;
textField.userInteractionEnabled = YES;
textField.keyboardType = UIKeyboardTypeNumberPad;
NSLog(@"Value = %@",textField.text);
[cell.contentView addSubview:textField];
[self.fields addObject:textField];
}
}
}
break;

case 6:
{
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifierF] autorelease];
cell.backgroundColor = [[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"reminderbuttonxl.png"]];

//Only add content to cell if it is new
if([cellIdentifierF isEqualToString: seventhCellIdentifier])
{
cell.backgroundColor = [UIColor clearColor];
textView.clipsToBounds = YES;
textView = [[UITextView alloc]initWithFrame: CGRectMake(-2, -3, 307, 154)];
UIImageView *imgView = [[[UIImageView alloc]initWithFrame: textView.frame]autorelease];
imgView.image = [UIImage imageNamed: @"buttonbg_text.png"];
[textView addSubview: imgView];
[textView sendSubviewToBack: imgView];
textView.backgroundColor = [UIColor clearColor];
textView.delegate = self;
textView.tag = 11;
tagValues = textView.tag;
textView.textColor = [UIColor purpleColor];
[cell.contentView addSubview:textView];
textView.text = @"Your birthday wishes/other reminder body here";
NSLog(@"Value = %@",textView.text);
}
}
}
break;

case 7:
{
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifierF] autorelease];
cell.backgroundColor = [[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"buttonbg-1.png"]];

//Only add content to cell if it is new
if([cellIdentifierF isEqualToString: eightCellIdentifier])
{
textField.tag = 107;
textField.inputView = self.groupPicker;
tagValues = textField.tag;
textField.text = reminderInstance.remGroup;
NSLog(@"Value = %@",textField.text);
[cell.contentView addSubview:textField];
[self.fields addObject:textField];
}
}
}
break;

default:
break;
}

int size = [fields count];
NSLog(@"there are %d objects in the array", size);

return cell;
}

我已经多次尝试找出到底出了什么问题,我还检查了一些问题herethere .但找不到任何有效/合适的。回答或解决我的问题。

所以请任何人帮助我提供有值(value)的建议或示例代码片段。

提前感谢大家:)

最佳答案

构建表单的方式会产生很多问题,这是有道理的(实际上就是这种情况)当我需要构建这样的表单时,我自己一直在做的是使用构建一个包含所有控件的普通 View (无论它有多长)并将该表单放在 ScrollView 中,并使用 View 的大小设置其内容大小在里面。这将使您能够更好地控制表单的外观。而且我认为我还没有见过使用表格 View 作为表单条目的应用程序。

你可以使用界面构建器来构建你的表单(只需欺骗 Xcode 并使主视图和 ScrollView 足够大,这样你就可以看到构建器上的所有内容,然后在完成后将其调整为适当的大小)

关于iphone - 滚动之前加载 UITableView 中的所有单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9414746/

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