2014-12-24 20 views
0

我有一个tableview。我想从选定的单元格名称创建字符串。例如我的单元名称:行号1 - 行号2 - 行号3 - 行号4 - 行号5选中行号1 - 行号2 - 行号5 mystring值=选定项目行号1,行号2,Row No 5.当我取消选择Row No 1 mystring value = Selected items Row No 2,Row No 5或Row No 5,Row No 2.没关系。 这可能吗?如果可能,我该怎么办?谢谢。Xcode MultipleCellSelection选择名称

//编辑 我的代码在这里;

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
// Do any additional setup after loading the view. 
tableData = [[NSMutableArray alloc] init]; 
[tableData retain]; 

for (int i=1; i<=10; i++) 
{ 
    NSString *rowInString= [[NSString alloc ]initWithFormat:@"Row No %d",i]; 
    [tableData addObject:rowInString]; 
    rowInString=nil; 
} 



_mytable.allowsMultipleSelectionDuringEditing = YES; 

    } 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
     NSLog(@"self.Data count: %lu", (unsigned long)[tableData count]); 
    return tableData.count ; 

} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *[email protected]"cell"; 
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellidentifier]; 
if(cell==nil) 
{ 
    cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentifier]; 
} 
NSString *cellValue; 


cellValue=[tableData objectAtIndex:[indexPath row]]; 

    [_mytable setEditing: YES]; 


cell.textLabel.text=cellValue; 



return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

NSString *selectedText = @"Selected Items"; 
NSArray *selectedRows = [_mytable indexPathsForSelectedRows]; 
NSLog(@"%lu" , (unsigned long)selectedRows.count); 
} 



- (void)dealloc { 
[_mytable release]; 
[super dealloc]; 
} 
@end 
+0

是的,这是可能的。 –

+0

@LyndseyScott我该怎么做。你可以分享示例代码吗?谢谢。 –

+1

你必须在人们帮助你之前展示你想要达到的目标。 – MattD

回答

0

先加:如果存在从字符串中删除它

NSRange check = [selectedText rangeOfString:@" one"]; 

if (check.location != NSNotFound) { 
selectedText = [selectedText stringByReplacingCharactersInRange:check withString:@""]; 
} 

其他使用

selectedText=[selectedText stringWithFormat:@" %@",cellName]; 

这里是你的代码(我已经改变了按您的要求),将其添加到字符串在你的tableview中。

self.myTableView.allowsMultipleSelection = YES; 

然后

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //you need an array in which you can add and remove strings 

    //adding the selected row string to array. 
    [stringArray addObject:[tableData objectAtIndex:[indexPath row]]; 
    NSString *string; 
    for(NSString *str in stringArray) 
    { 
    string = [NSString stringWithFormat:"%@%@",string,str]; 
    } 
    NSLog(@"Selected Row %@"string); 
} 

- (void)tableView:(UITableView *)tableView didDeSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //removing the selected string from array 

    for(NSString *stringInArray in stringArray) 
    { 
     if([stringInArray isEqualToString:[tableData objectAtIndex:[indexPath row]]) 
     { 
      [stringArray removeObject:stringInArray]; 
     } 
    } 

    NSString *string; 
    for(NSString *str in stringArray) 
    { 
    string = [NSString stringWithFormat:"%@%@",string,str]; 
    } 
    NSLog(@"Selected Row %@"string); 
} 
+0

@FawarMasud谢谢。是工作。我创建NSMuttableArray。但我改变了一些线。但是,当我取消选择不删除字符串。 –

+0

你使用相同的代码?我的意思是你没有使用“==”而不是“isEqualToString”? –

+0

是相同的代码。我的didDeselect方法不起作用。我使用NSLog(@“取消选择”);当我取消选择单元格我不能看到登录日志控制台。 –

0

这里是你如何能做到这一点: 创建的NSString * selectedText = @ “所选项目”; 现在,当你在tableview中按下一个项目时,didselectitematibdexpath将被调用。 使用indexpath获取单元格的名称。现在比较一下,如果该文本存在于selectedText字符串中。

NSString *selectedText = @"Selected Items"; 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
    tableData = [[NSMutableArray alloc] init]; 
    [tableData retain]; 

    for (int i=1; i<=10; i++) 
{ 
    NSString *rowInString= [[NSString alloc ]initWithFormat:@"Row No %d",i]; 
    [tableData addObject:rowInString]; 
    rowInString=nil; 
} 



_mytable.allowsMultipleSelectionDuringEditing = YES; 

    } 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
     NSLog(@"self.Data count: %lu", (unsigned long)[tableData count]); 
    return tableData.count ; 

} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *[email protected]"cell"; 
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellidentifier]; 
if(cell==nil) 
{ 
    cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentifier]; 
} 
NSString *cellValue; 


cellValue=[tableData objectAtIndex:[indexPath row]]; 

    [_mytable setEditing: YES]; 


cell.textLabel.text=cellValue; 



return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
NSRange check = [selectedText rangeOfString:[tableData objectAtIndex:[indexPath row]]]; 

if (check.location != NSNotFound) { 
    selectedText = [selectedText stringByReplacingCharactersInRange:check withString:@""]; 
} else { 
    selectedText = [NSString stringwithFormat:@"%@ %@",selectedText, [tableData objectAtIndex:[indexPath row]]]; 
} 
NSArray *selectedRows = [_mytable indexPathsForSelectedRows]; 
NSLog(@"%lu" , (unsigned long)selectedRows.count); 
} 



- (void)dealloc { 
[_mytable release]; 
[super dealloc]; 
} 
@end 
+0

在行rangeOfString而不是@“one”提供celName –

+0

我试试。但我不能。我分享我的代码。你可以编辑。谢谢。 –

+0

我已在上面的代码中进行了更新。请立即检查。 –