【问题标题】:Xcode MultipleCellSelection Selection namesXcode MultipleCellSelection 选择名称
【发布时间】:2014-12-24 21:02:14
【问题描述】:

我有一个表格视图。我想从选定的单元格名称创建字符串。 For example My Cell names : Row No 1 - Row No 2 - Row No 3 - Row No 4 - Row No 5. When selected Row No 1 - Row No 2 - Row No 5 mystring value = Selected Items Row No 1 , Row No 2,第 5 行。当我取消选择第 1 行时,我的字符串值 = 所选项目第 2 行,第 5 行或第 5 行,第 2 行。没关系。 是否可以?如果可能的话我该怎么办?谢谢。

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

- (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 *cellidentifier=@"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

【问题讨论】:

  • @LyndseyScott 我该怎么做。你能分享示例代码吗?谢谢。
  • 在人们帮助你之前,你必须展示你为达到你想要的目标所做的努力。
  • 如果用户选择五 - 二 - 三,文本应该是什么?会是五二三还是二三五
  • 没关系。只是我想看到相同的字符串。
  • 在下面查看我的答案。它适用于多项选择。

标签: ios iphone multipleselection


【解决方案1】:

首先添加你的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);
}

【讨论】:

  • @FawarMasud 谢谢。这是工作。我创建 NSMuttableArray。但我改变了一些行。但是当我取消选择不删除字符串时。
  • 您使用的是相同的代码吗?我的意思是你没有使用“==”而不是“isEqualToString”?
  • 是的,相同的代码。我的 didDeselect 方法不起作用。我使用 NSLog(@"Deselect");当我取消选择单元格时,我在日志控制台中看不到日志。
  • 你设置单元格选择样式了吗?如果没有,请在代码中添加 cell.selectionStyle = UITableViewCellSelectionStyleGray。还要检查表视图的代表。使用断点。
  • 发生了什么我不知道。我再次编写所有代码并且它有效。谢谢@FawadMasud。
【解决方案2】:

您可以这样做: 创建一个 NSString *selectedText=@"Selected Items"; 现在,当您在 tableview 中按下一个项目时,didselectitematibdexpath 将被调用。 使用 indexpath 获取单元格的名称。现在比较该文本是否存在于 selectedText 字符串中。如果存在,将其从字符串中删除

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

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

否则将其添加到字符串中

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

这是您的代码(我已根据您的要求进行了更改):

 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 *cellidentifier=@"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

【讨论】:

  • 就行 rangeOfString 而不是 @"one" 提供 celName
  • 我试试。但我做不到。我分享我的代码。可以编辑吗。谢谢。
  • 我已经更新了你上面的代码。请立即检查。
  • 谢谢。但是当我选择两个单元格时,这行 NSRange check = [selectedText rangeOfString:[tableData objectAtIndex:[indexPath row]]];说 Thread1: EXC_BAD_ACCESS (code=EXC_1386_GPFLT)
  • 你的意思是你有多项选择?
猜你喜欢
  • 1970-01-01
  • 2020-07-27
  • 2015-12-11
  • 2021-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-18
相关资源
最近更新 更多