0

I have a dynamic table view with 2 section, and 4 rows in each which are populated by arrays.

Only one cell should be checked, at the moment none are. I know the text for the cell I want checked.

So my question is, can I set the accessoryType for a cell based on the text that cell contains?

Like IF cell.textLabel.text equals THIS then set that cell accessoryType check.

Edit

The code I have so far, taking into account comments below is this:

  switch (indexPath.section) {
        case standardSection:
            serverLoc.textLabel.text = [self.serverSelection objectAtIndex:indexPath.row];
            if ([[self.serverSelection objectAtIndex:indexPath.row] rangeOfString:cellValue].location != NSNotFound) {
                serverLoc.accessoryType = UITableViewCellAccessoryCheckmark;
            }
            break;
        case qualitySection:
            serverLoc.textLabel.text = [self.qServerSelection objectAtIndex:indexPath.row];
            if ([[self.qServerSelection objectAtIndex:indexPath.row] rangeOfString:cellValue].location != NSNotFound) {
                serverLoc.accessoryType = UITableViewCellAccessoryCheckmark;
            }
            break;
        default:
            break;
    }

The sections set them selves up like this:

Section 1:

Chicago, IL London, UK San Jose, CA Washington, DC

Section 2:

Chicago, IL (Q) London, UK (Q) San Jose, CA (Q) Washington, DC (Q)

As you can see I'm checking against the value of CellValue. CellValue is set using a delegate from the previous view.

Let's say this time is equals London, UK. When I come into this view both London, UK and London, UK (Q) are checked, I only want the one without (Q) checked. Even using "isEqualToString" this occurred. If cellValue equals London, UK (Q) then this works.

halfer
  • 19,824
  • 17
  • 99
  • 186
Dan
  • 2,304
  • 6
  • 42
  • 69

2 Answers2

2

Do this in cellForRowAtIndexPath. After you have created your cell just do a check on the value in the array (assuming the array contains NSStrings). So:

if ([[self.myArray objectAtIndex:indexPath.row] isEqualToString:@"the string I want"]) {
     cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

Edit: Based on your comment all strings will start with the same thing but may have differences. You could do something like this:

if ([[self.myArray objectAtIndex:indexPath.row] rangeOfString:@"London,"].location != NSNotFound) {
     cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

This would say if the NSString contains "London," then evaluate to YES. Or in other words, the NSString contains "London,"

Firo
  • 15,448
  • 3
  • 54
  • 74
  • sorry for the delay in getting back to you! Had other troubles but came back to this and yes, it works...in a way. The issue being some cells are similar but for the end letter. So let's say I have "London, UK" and "London, UK Q". Both will be checked as they both CONTAIN London, UK. Anyway to make the isEqualToString exact rather than contains? – Dan Feb 01 '13 at 04:43
  • I updated my answer, see if that helps! If it does please don't forget to accept the answer! – Firo Feb 01 '13 at 05:04
  • I'll expand a bit: London, UK, San Jose, CA, Washington, DC and Chicago, IL make up 4 cells in one section. The second section has the same but with (Q) at the end. Even with your changes both cells will have a check mark if they contain London or Chicago, does that make sense? – Dan Feb 01 '13 at 05:06
  • Sounds good. :) Now I'm a bit confused though. With the edited answer that would checkmark anything that starts with London, so "London, UK" and "London, UK Q"... With the first implementation it would only do exact matches. In the set you gave me what are you trying to check? Anything with London and Chicago or just London? In both sections or only the first one? – Firo Feb 01 '13 at 05:20
  • Hold the phone, it now works. I previously didn't have them in the switch statement. Since moving them I tried going back to isEqualString and it works now....Thank you, and accepted! – Dan Feb 01 '13 at 05:35
0

You should try to set that value when you're creating the cell (from your 'model' not by looking at the cell's label).

If some circumstance causes the value to change, you should update your model (the data you use when creating each cell); then call [self.tableview reloadData].

Dave
  • 7,552
  • 4
  • 22
  • 26
  • I'm using dynamic cells so they all have the same identifier so can only set the accessoryType once the view has loaded – Dan Feb 01 '13 at 01:04
  • @Dave, you do not want to set the accessoryType anywhere outside of cellForRowAtIndexPath. – Firo Feb 01 '13 at 01:08
  • You'll want/need to set the value in `cellForRowAtIndexPath`. – Dave Feb 01 '13 at 01:09
  • 1
    I wouldn't even if you could. People will expect to find that code in cellForRowAtIndexPath. Also, ideally always look at your model when deciding how to create each cell; and not at the cell's contents. My 2 cents. I hope it help you. :) – Dave Feb 01 '13 at 01:12