0

I am new in iOS and I am facing problem to add multi select table value to text field. My code is like this In DidSelectRowAtIndexPath

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
 txtactivity.text=[activityarray objectAtIndex:indexPath.row];
        lblactivity.text=[NSString stringWithFormat:@"%@",[idActivityarray objectAtIndex:indexPath.row]];
        txtactivity.text=[NSString stringWithFormat:@"%@",[activityarray objectAtIndex:indexPath.row]];
        [[tableactivity cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
}

I am getting output like this

enter image description here

CellForRowAtIndexPath

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text=[NSString stringWithFormat:@"%@",[activityarray objectAtIndex:indexPath.row]];

    return cell;
}

My problem is I am getting only one value in the text box. I need to get all the selected values in the text box. How to do this? Thanks in Advance!

Muju
  • 884
  • 20
  • 54

3 Answers3

3

Take a NSMutableArray and initalize it at viewDidLoad. Then use the following code.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   if(!arrSelectedValue containsObject:[activityarray objectAtIndex:indexPath.row]){
     [arrSelectedValue addObject:[activityarray objectAtIndex:indexPath.row]];
     txtactivity.text = [arrSelectedValue componentsJoinedByString:@","];
   }

   [[tableactivity cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
}

- (void)tableView:(UITableView *)tableView didDeSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   if(arrSelectedValue containsObject:[activityarray objectAtIndex:indexPath.row]){
     [arrSelectedValue removeObject:[activityarray objectAtIndex:indexPath.row]];
     txtactivity.text = [arrSelectedValue componentsJoinedByString:@","];
   }

   [[tableactivity cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryNone];
}
Poles
  • 3,585
  • 9
  • 43
  • 91
  • @Muju : Actually this has nothing to do with scrolling. I think may be you are not using `dequeueReusableCellWithIdentifier` which leads to reallocating cell every time and the laggy scrolling. – Poles Jan 30 '17 at 10:14
  • I am using dequeueReusableCellWithIdentifier in cellforrowatindexpath – Muju Jan 30 '17 at 10:18
  • Can you show me `cellForRowAtIndexPath` method's code? – Poles Jan 30 '17 at 10:19
  • The code looks right. What problem are you getting while scrolling? – Poles Jan 30 '17 at 10:27
  • When I scroll below cell also get selected – Muju Jan 30 '17 at 10:28
  • Ok. Do one thing write this code inside cellForRowAtIndex before `return cell` - `if(arrSelectedValue containsObject:[activityarray objectAtIndex:indexPath.row]){[cell setAccessoryType:UITableViewCellAccessoryCheckmark];}else{ [cell setAccessoryType:UITableViewCellAccessoryNone];}` – Poles Jan 30 '17 at 10:37
  • Can you Please answer my question http://stackoverflow.com/questions/43539307/how-to-post-string-with-special-character-and-thai-language-using-xml-parsing-in – Muju Apr 22 '17 at 06:39
2

try this one, without storing into array directly you can append the string as per your selection. I hope this will work for you.

[txtactivity setText:[[txtactivity text] stringByAppendingString:[NSString stringWithFormat:@", %@", [activityarray objectAtIndex:indexPath.row]]]];
Antony Raphel
  • 2,036
  • 2
  • 25
  • 45
0

I hope txtactivity is your textfield. In your code, you are just setting the textfield text as the selected value.It will replace the current text. Instead of that append the selected value with the textfield content. Change the code

txtactivity.text=[NSString stringWithFormat:@"%@",[activityarray objectAtIndex:indexPath.row]];

to this

txtactivity.text=[NSString stringWithFormat:@"%@,%@",txtactivity.text,[activityarray objectAtIndex:indexPath.row]]

This will append the newly selected value to the content of textfield.

AloSwift
  • 407
  • 6
  • 24
  • if I select 3 value then it overrite. – Muju Jan 30 '17 at 07:08
  • It won't over write anything unless You are setting text (txtactivity.text = codes.. ) some where else. You need to remove all othet txtactivit.text codes. Also in addition to my point, if you need to handle de-selcting row, I would suggest to go for the solution suggested by "Poles" :) – AloSwift Jan 30 '17 at 07:14