我有一個UITableView,其中有8個單元格(部分),其中6個包含textFields作爲子視圖,2個包含按鈕,另一個包含文本視圖。現在我有一個問題在這裏,因爲我們都知道,當視圖只出現時,可見單元格加載。因此,當我的應用程序加載時,我能夠查看5個單元格,前4個包含文本字段和第5個單元格與按鈕。可以看到陣列計數爲4,當我顯示它通過測井控制檯即在滾動之前加載UITableView中的所有單元格
there are 4 objects in the array
現在滾動表視圖後,我可以觀察到以下:
there are 6 objects in the array
因此,發生了什麼是如果我要保存在表視圖中輸入的數據,或者說編輯以更改現有數據,我不得不滾動表視圖並在最後一個單元格中輸入值。對於用戶來說,情況並非如此,因爲我們不能指望他/她滾動表格視圖並輸入完整的表格/表格視圖單元格條目。他/她做出了什麼改變,只需點擊完成/保存了什麼它永遠是..!
更多我有一個選擇器視圖作爲包含文本字段作爲子視圖的最後一個單元格的輸入視圖。在我的選擇器視圖做選擇行方法我面臨着最後一個細胞/部分(textField作爲子視圖)選擇器視圖這裏是我的方法的代碼片段:
-(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...
我不知道爲什麼,從選擇器視圖(最後一節電池)選擇值後有時死機一些時間,有時效果fine.Some次崩潰在上述行註釋case 107,有時在主autorelease池中。有些時候線程如下圖所示:
那麼,有沒有我們可以加載所有細胞一下子讓所有的文本字段被添加到陣列之前scrolling.There不能有任何的問題,我相信
更多詳細編輯的代碼以任何方式對於理解:
- (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;
}
我曾幾次試圖找出究竟是怎麼了?也是我查了一些問題here和there。但找不到任何EFFIC ient/APPR。回答或解決我的問題。
所以,任何人都可以幫助我提供寶貴的建議或示例代碼片段。
感謝所有提前:)
我不相信有可能加載不可見的單元格。該表將從數據源中提取數據並僅加載可見單元格。其他單元格將在您滾動時加載,並且它們將變爲可見。 – 2012-02-23 14:19:52
你在哪裏製作新單元如果它不存在 – fibnochi 2012-02-23 14:26:14
@BillBurgess謝謝布爾吉斯先生據我所知還知道你在說什麼..但是我面臨很多問題,因爲這樣做是因爲沒有做出任何改變或觸摸最後一個如果用戶進入表格視圖,填入在該時間點輸入的數據,然後說出用戶是否隨意修改了前2個單元格並單擊完成然後它將最後一個字段(不可見)值視爲零,即使有值。 – 2012-02-23 14:26:17