2014-12-24 23 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

我已在上面的代碼中進行了更新。請立即檢查。 –