我正在获得这样的NSMutableArray,
Selected Id : (
2,
92,
154
)
它不是静态的,当我选择多个表视图行时会添加这些值。
目前我选择了3行,其ID在该数组中打印, 如果我再选择一行,即4行,那么数组将是这样的
Slected Id : (
2,
92,
154,
12
)
如果我取消选择任何行,假设我取消选择2行,那么新数组将是
Slected Id : (
154,
12
)
所以,我想将这些值存储在任何会给我输出的对象中,
154,12
即用逗号分隔的对象
我选择了5个值,它必须像这样存储
6,87,154,65,45
如何实施?
我的项目代码:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
self.selectedPath = indexPath;
if ([tableView isEditing]) {
// [selectedArray addObject:[_mutableArray objectAtIndex:indexPath.row]];
[selectedArray addObject:[[_mutableArray objectAtIndex:indexPath.row] valueForKey:@"c_uid"]];
count1=(int)[selectedArray count];
NSLog(@"Selected count is :%i",count1);
NSLog(@"Slected Id : %@",selectedArray);
}else{
/// other action
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
self.selectedPath = indexPath;
// [selectedArray removeObject:[_mutableArray objectAtIndex:indexPath.row]];
[selectedArray removeObject:[[_mutableArray objectAtIndex:indexPath.row] valueForKey:@"c_uid"]];
count1=(int)[selectedArray count];
NSLog(@"Selected count is :%i",count1);
NSLog(@"Slected Id : %@",selectedArray);
if (!selectedArray.count) {
[self.tableView setEditing:NO animated:YES];
}
}
在这里,我将获得所选数据,
NSLog(@"Slected Id : %@",selectedArray);
答案 0 :(得分:0)
为此,您应该使用以下UITableView
的委托方法,
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
//your code...
}
现在通过采用一个可变数组来管理选择和取消选择行ID,如下所示
var selectedRowArr = [String]() // Change if your id is Int or other type.
现在您需要更新上面的代理方法,如下所示
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
//your code...
// First check "rowId" already added or not
if selectedRowArr.contain(rowId){ // make sure "rowId" is same type of array
selectedRowArr = selectedRowArr.filter { $0 != rowId as! String } // remove object from array list
}
else {
selectedRowArr.append(rowID)
}
}
将数组转换为字符串
let myString = selectedRowArr.joined(separator: ",")
输出应为
<强> 1,12,13 强>
更新:
首先删除didDeselectRowAtIndexPath
方法我将修改您的didSelectRowAtIndexPath
方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
self.selectedPath = indexPath;
if ([tableView isEditing]) {
First you need to check "c_uid" is already added in the Array or not ?
if ([selectedArray containsObject:someObject]) {
[selectedArray removeObject:[[_mutableArray objectAtIndex:indexPath.row] valueForKey:@"c_uid"]];
count1=(int)[selectedArray count];
NSLog(@"Selected count is :%i",count1);
NSLog(@"Slected Id : %@",selectedArray);
if (!selectedArray.count) {
[self.tableView setEditing:NO animated:YES];
}
}
else {
[selectedArray addObject:[[_mutableArray objectAtIndex:indexPath.row] valueForKey:@"c_uid"]];
count1=(int)[selectedArray count];
NSLog(@"Selected count is :%i",count1);
NSLog(@"Slected Id : %@",selectedArray);
}
}else{
/// other action
}
如果要将数组转换为字符串,则
NSString * result = [selectedArray componentsJoinedByString:@","];
输出应为
<强> 1,12,13 强>
答案 1 :(得分:0)
希望以下代码对您有所帮助!
NSMutableString *strProductId = [[NSMutableString alloc]init];
[strProductId setString:@""];
NSArray *arrTemp = [selectedArray copy];
for (int i=0; i<[arrTemp count]; i++)
{
if ([arrTemp count]-1 == i)
{
[strProductId appendFormat:@"%@",[arrTemp objectAtIndex:i]];
}
else
{
[strProductId appendFormat:@"%@,",[arrTemp objectAtIndex:i]];
}
}
NSLog(@"Selected Id : %@",strProductId);
输出可能是!
所选标识:6,87,154,65,45