我有一个 TableView,它有 2 个部分,只有在选择第一部分的第 3 行时才会显示第 2 部分。只有第一部分有图像。一切正常。第 2 部分的第 3 行在选中时显示一个 TextField 和一个按钮,您可以通过按完成按钮在其中保存一个日期。工作也很好。您可以选择该行并转到其他行,一切正常。但是,如果您点击 TextField 并且不保存输入或不写任何内容,那么它就会出错。它将重新加载带有随机行图像的表格。我以为是弹出的键盘,所以我尝试手动将其关闭。错误仍然存在。我真的很好奇为什么会发生这种情况,所以我希望得到帮助!提前致谢!!
我的 cellForRowAtIndexPath:
- (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];
}
// Configure the cell...
BOOL tooltipValue = [[NSUserDefaults standardUserDefaults] boolForKey:@"tooltipSetting"];
BOOL compactSettingValue = [[NSUserDefaults standardUserDefaults] boolForKey:@"compactModeSetting"];
NSDictionary *dictionary = [self.settingsArray objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:@"Items"];
NSString *cellValue = [array objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
if (indexPath.section == 0 && self.iconSetCount < 3) {
NSDictionary *imagedictionary = [self.imagesArray objectAtIndex:indexPath.section];
UIImage *cellImage = [[imagedictionary objectForKey:@"images"] objectAtIndex:indexPath.row];
cell.imageView.image = cellImage;
self.iconSetCount++;
NSLog(@"%d",self.iconSetCount);
}
if (indexPath.section == 0){
if (indexPath.row == 0) {
if (tooltipValue){
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
if (indexPath.row == 1) {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if (indexPath.row == 2) {
if (!compactSettingValue){
cell.accessoryType = UITableViewCellAccessoryNone;
} else {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
} else if (indexPath.section == 1){
// if the sellected row is the one with a custom date
if (indexPath.row == 2 && indexPath.row == isSelected) {
// if an archive exists for the custom date then get the data from the archive
if ([[NSFileManager defaultManager] fileExistsAtPath:[self getDocumentPathForCustomDate]]) {
NSString *customDate = [NSKeyedUnarchiver unarchiveObjectWithFile:[self getDocumentPathForCustomDate]];
cell.textLabel.text = customDate;
} else{
//show a label with a done button where you can type in your custom date and save it
if (self.textField == nil) {
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(5, 5, 200, 35)];
self.textField.backgroundColor = [UIColor whiteColor];
self.textField.textColor = [UIColor blackColor];
self.textField.font = [UIFont systemFontOfSize:16.0];
self.textField.borderStyle = UITextBorderStyleRoundedRect;
}
if (self.button == nil) {
self.button =[UIButton buttonWithType:UIButtonTypeRoundedRect];
self.button.frame = CGRectMake(210, 5, 50, 35);
[self.button setTitle:NSLocalizedString(@"done", @"") forState:UIControlStateNormal];
[self.button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
}
[cell.contentView addSubview: self.textField];
[cell.contentView addSubview: self.button];
}
}
if ((indexPath.row == self.isSelected)){
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else{
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
return cell;
}
我的 didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"Nib name" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
*/
BOOL tooltipValue = [[NSUserDefaults standardUserDefaults] boolForKey:@"tooltipSetting"];
BOOL compactSettingValue = [[NSUserDefaults standardUserDefaults] boolForKey:@"compactModeSetting"];
if (indexPath.section == 0) {
//tooltips
if (indexPath.row == 0) {
if (!tooltipValue){
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"tooltipSetting"];
} else {
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"tooltipSetting"];
}
[self.tableView reloadData];
}
//go to Set your Signature Extra
if (indexPath.row == 1) {
AdditionalSignature *additionalSigi = [[AdditionalSignature alloc] initWithNibName:@"additionalSignature" bundle:nil];
[self.navigationController pushViewController:additionalSigi animated:YES];
[additionalSigi release];
}
//compact version
if (indexPath.row == 2) {
if (!compactSettingValue){
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"compactModeSetting"];
} else {
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"compactModeSetting"];
}
[self.tableView reloadData];
}
}
// if in optional compactmode settings
if (indexPath.section == 1){
// and "none" is selected remove first the custom date
if (indexPath.row == 0) {
[self removeCustomDateArchive];
//then change the userdefault for this setting
if (![self isOptionNoDate]) {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"dateSetting"];
}else{
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"dateSetting"];
}
}
if ( indexPath.row == 1) {
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"dateSetting"];
[self removeCustomDateArchive];
}
if (indexPath.row == 2) {
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"dateSetting"];
}
self.isSelected = indexPath.row;
[self.tableView reloadData];
}
}