gpt4 book ai didi

iphone - 跟踪选定的单元格

转载 作者:行者123 更新时间:2023-11-29 13:24:20 27 4
gpt4 key购买 nike

在我的应用程序中,用户根据他/她选择的单元格更改出现在 tableView 中的字段。 (仅供引用...我允许选择多个单元格。)当用户按下后退按钮时,程序会将所选单元格的 textLabel 复制到父 viewController 的占位符。

这是我的代码的相关部分:

- (void)willMoveToParentViewController:(UIViewController *)parent
{
int tempCount = 0;

for (int section = 0; section < 3; section++)
{
int rowMax;

if (section == 0)
rowMax = 3;

else if (section == 1)
rowMax = 5;

else if(section == 2)
rowMax = 3;

for(int row = 0; row < rowMax; row++)
{
NSIndexPath *tempIndexPath = [NSIndexPath indexPathForRow:row inSection:section];

UITableViewCell *selectedCell = [self.tableView cellForRowAtIndexPath:tempIndexPath];

if(selectedCell.accessoryType == UITableViewCellAccessoryCheckmark)
{
NSLog(@"tempCount = %d", tempCount);
tempCount++;

if (tempCount == 1)
chosenFieldsViewController.field0.placeholder = selectedCell.textLabel.text;

else if(tempCount == 2)
chosenFieldsViewController.field1.placeholder = selectedCell.textLabel.text;

else if(tempCount == 3)
chosenFieldsViewController.field2.placeholder = selectedCell.textLabel.text;

}
}
}
}

我意识到,如果向下滚动 tableView,选择单元格后,所选单元格不会在 parentVC 上显示为 placeHolder。根据我的分析,我认为一旦向下滚动,单元格就会从内存中删除。因此,尽管选择了单元格,但它们无法显示在父 VC 上。

如果是这样,为什么我在向上滚动时看到单元格显示为选中状态?

如果有人可以建议我如何在用户滚动时跟踪所选单元格,我将不胜感激。

谢谢。

编辑 1

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];

if(selectedCell.accessoryType != UITableViewCellAccessoryCheckmark && count<3)
{
selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;
count ++;
}

else if(selectedCell.accessoryType == UITableViewCellAccessoryCheckmark)
{
selectedCell.accessoryType = UITableViewCellAccessoryNone;
count--;
}

}

编辑2

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

NSString *CellIdentifier = [NSString stringWithFormat:@"CellForRow%dSection%d",indexPath.row, indexPath.section];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}

cell.selectionStyle = UITableViewCellSelectionStyleNone; // VERY IMPORTANT

if (indexPath.section == 0)
{
switch(indexPath.row)
{
case 0:
cell.textLabel.text = @"Class A";
break;

case 1:
cell.textLabel.text = @"Class B";
break;

case 2:
cell.textLabel.text = @"Class C";
break;

default:
break;
}
}

if(indexPath.section == 1)
{
switch(indexPath.row)
{
case 0:
cell.textLabel.text = @"Class D";
break;

case 1:
cell.textLabel.text = @"Class E";
break;

case 2:
cell.textLabel.text = @"Class F";
break;

case 3:
cell.textLabel.text = @"Class G";
break;

case 4:
cell.textLabel.text = @"Class H";
break;

default:
break;
}
}

if(indexPath.section == 2)
{
switch (indexPath.row)
{
case 0:
cell.textLabel.text = @"Class I";
break;

case 1:
cell.textLabel.text = @"Class J";
break;

case 2:
cell.textLabel.text = @"Class K";
break;

default:
break;
}
}

return cell;
}

最佳答案

将字典声明为,

@property (nonatomic, retain) NSMutableDictionary *selectedTextListDict;

在viewDidLoad中,

NSMutableDictionary *selectedTextListDict = [[NSMutableDictionary alloc] init];

然后将这些方法更改为,

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];

if(selectedCell.accessoryType != UITableViewCellAccessoryCheckmark && count < 3)
{
NSString *rowKeyString = [NSString stringWithFormat:@"%d", indexPath.row];
NSString *sectionKeyString = [NSString stringWithFormat:@"%d", indexPath.section];

NSMutableDictionary *row = [self.selectedTextListDict valueForKey:sectionKeyString];
if (!row) {
row = [[NSMutableDictionary alloc] init];
}
[row setObject:selectedCell.textLabel.text forKey:rowKeyString];
[self.selectedTextListDict setObject:row forKey:sectionKeyString];

selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;
count ++;
}

else if(selectedCell.accessoryType == UITableViewCellAccessoryCheckmark)
{

NSString *rowKeyString = [NSString stringWithFormat:@"%d", indexPath.row];
NSString *sectionKeyString = [NSString stringWithFormat:@"%d", indexPath.section];

NSMutableDictionary *row = [self.selectedTextListDict valueForKey:sectionKeyString];
[row removeObjectForKey:rowKeyString];

if ([[row allKeys] count] == 0) {
[self.selectedTextListDict removeObjectForKey:sectionKeyString];
} else {
[self.selectedTextListDict setObject:row forKey:sectionKeyString];
}

selectedCell.accessoryType = UITableViewCellAccessoryNone;
count--;
}
}


- (void)willMoveToParentViewController:(UIViewController *)parent
{
NSArray *selectedTextSectionKeysList = [self.selectedTextListDict allKeys];
NSArray *sortedSelectedTextSectionKeysList = [selectedTextSectionKeysList sortedArrayUsingSelector:@selector(intValue)];
int tempCount = 0;

for (NSString *sectionString in sortedSelectedTextSectionKeysList) {

NSMutableDictionary *rowDict = [self.selectedTextListDict valueForKey:sectionString];

if (rowDict) {

NSArray *selectedTextRowKeysList = [rowDict allKeys];
NSArray *sortedSelectedTextRowKeysList = [selectedTextRowKeysList sortedArrayUsingSelector:@selector(intValue)];

for (NSString *rowString in sortedSelectedTextRowKeysList) {

tempCount++;

if (tempCount == 1)
chosenFieldsViewController.field0.placeholder = [rowDict valueForKey:rowString];

else if(tempCount == 2)
chosenFieldsViewController.field1.placeholder = [rowDict valueForKey:rowString];

else if(tempCount == 3)
chosenFieldsViewController.field2.placeholder = [rowDict valueForKey:rowString];
}

}
}
}

编辑 1:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:@"GoToModifyFieldsViewController"])
{
ModifyFieldsViewController *modifyFieldsViewController = segue.destinationViewController;
modifyFieldsViewController.chosenFieldsViewController = self;

field0.placeholder = @"";
field1.placeholder = @"";
field2.placeholder = @"";

if(self.selectedTextListDict)
self.selectedTextListDict = [[NSMutableDictionary alloc] init];
}
}

ChosenFieldsViewController 中声明一个字典:as,

@property (nonatomic, retain) NSMutableDictionary *selectedTextListDict;

在viewDidLoad中,

selectedTextListDict = [[NSMutableDictionary alloc] init];

因此,与其使用 self.selectedTextListDict,不如使用: ModifyFieldsViewController 中的 chosenFieldsViewController.selectedTextListDict

关于iphone - 跟踪选定的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13521399/

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