Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将文本颜色更改为单击的行,并将其保留在内存中_Ios_Objective C_Xcode - Fatal编程技术网

Ios 将文本颜色更改为单击的行,并将其保留在内存中

Ios 将文本颜色更改为单击的行,并将其保留在内存中,ios,objective-c,xcode,Ios,Objective C,Xcode,当用户单击TableView中的一行时,文本颜色会发生变化 我的问题是如何在内存中保存单击的行,以及何时返回TableView以使用新颜色保存该行 这在CellForRowatineXpath中: NSAttributedString * redString = [[NSAttributedString alloc] initWithString:cell.textLabel.text]; NSMutableAttributedString * attributedString = [[NSM

当用户单击TableView中的一行时,文本颜色会发生变化

我的问题是如何在内存中保存单击的行,以及何时返回TableView以使用新颜色保存该行

这在CellForRowatineXpath中:

NSAttributedString * redString = [[NSAttributedString alloc] initWithString:cell.textLabel.text];

NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithAttributedString:redString];

NSRange boldedRange = NSMakeRange(0, redString.length);

[attributedString addAttribute: NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-LightItalic" size:15.0] range:boldedRange];

[attributedString addAttribute: NSForegroundColorAttributeName value: [UIColor redColor] range:boldedRange];

[cell.textLabel setAttributedText: attributedString];

提前感谢

我建议在UITableViewController中保留一个NSMutableDictionary,用于跟踪已选择的单元格。然后,无论何时使用didSelectRowAtIndexPaht选择单元格,都可以更新字典:

范例

-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
    NSIndexPath *idxKey = [NSIndexPath indexForRow:indexPath.row inSection:indexPath.section];
    self.myMutableDictionary[idxKey] = @YES;
} 
鉴于方法签名中提供了nsindepath,因此必须创建nsindepath,这似乎有些奇怪。问题是,有时它实际上是一个可变的索引路径,并且会失败。通过这种方式创建副本,可以保证它是不可变的密钥

拥有字典后,只需在tableView:cellForRowAtIndexPath中检查字典,查看是否已选中它

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSIndexPath *idxKey = [NSIndexPath indexForRow:indexPath.row inSection:indexPath.section];
    id value = self.myMutableDictionary[idxKey];
    if(value){
      // Set color selected
     } else {
      // Set color not selected
    } 
}