0

I have a .h file I am using to define all my enum lists, I now would like to use one of the lists to populate a UITableView that I have created in a UITableViewController class. However I am not sure how to read the enum list into the UITableViewController class?

This is what my enum list looks like.

// NameNum
typedef enum {John = 0, Jerry = 1, Jack = 2, Jeff = 3, Jonny = 4} Namenum;

I would like to show the names in the UITableView.

Update: The reason I have used enums is because when a name from the UITableView is selected I need the number corresponding number to be sent in a request to a server call I am making.

halfer
  • 19,824
  • 17
  • 99
  • 186
HurkNburkS
  • 5,492
  • 19
  • 100
  • 183
  • What do you want to show in the table view? Each enum value's integer value? – rmaddy May 20 '14 at 04:16
  • create enum object like `Namenum obj`; then .m file `obj = John` or whatever. and you can compare it using `if(obj == John/0)` – ChintaN -Maddy- Ramani May 20 '14 at 04:17
  • @maddy I would like to show the names not the numbers... which now has me thinking I put these things around the wrong damn way :P – HurkNburkS May 20 '14 at 04:19
  • 1
    Does this SO Q&A help [enum Values to NSString (iOS)](http://stackoverflow.com/questions/6331762/enum-values-to-nsstring-ios)? Also this linked [Convert objective-c typedef to its string equivalent](http://stackoverflow.com/questions/1094984/convert-objective-c-typedef-to-its-string-equivalent)? – andrewbuilder May 20 '14 at 04:21
  • Show "names", try use NSArray contains these names. – nmh May 20 '14 at 04:23
  • @andrewbuilder I am looking thought it now.. still making sense of it... the biggest issue I Have is how to use the enum list like an array for instance so I can count the number of entries to be used as a row count for indexpath. – HurkNburkS May 20 '14 at 04:27
  • @HurkNburkS I suspect the answer by [Mark Longmire] (http://stackoverflow.com/users/933260/mark-longmire) will help you? Alternatively the `switch-case` solution provided by http://stackoverflow.com/users/104975/badmonkey? Mark's solution is more compact, and as far as I can tell achieves the same outcome. – andrewbuilder May 20 '14 at 04:31
  • @andrewbuilder The correct answer in this question http://stackoverflow.com/questions/6331762/enum-values-to-nsstring-ios/17597483#17597483 how would I provide that function the enum? when considering my enum is in a different.h file.. – HurkNburkS May 20 '14 at 04:34
  • I'm no expert in C. I guess you may need to package as an object to transfer between files? Perhaps parse the enum macro in the implementation file associated with your header file, then assign to public `NSArray` (mentioned above), then init your header in the destination TVC file so that you can grab a reference to its public `NSArray`. I'd need to see more code to offer a more detailed solution. – andrewbuilder May 20 '14 at 05:03
  • I guess u can directly do this in your .h file typedef enum {John = 0, Jerry = 1, Jack = 2, Jeff = 3, Jonny = 4} Namenum; #define getNamesFormIndex(index) [@[@"John",@"Jerry",@"Jack",@"Jeff",@"Jonny"] objectAtIndex:index] – Prathamesh Saraf May 20 '14 at 05:09
  • @PrathameshSaraf cool, I was trying to create like a global header that I could access from any other .m file just by using #import.. that way it makes for easy control of the enums when or if they need to be updated.. but I think Im just going to add the enum to the .h file of the UITableViewController to make things easier. – HurkNburkS May 20 '14 at 19:25
  • 1
    @HurkNburkS: Actually by .h i meant the global header . It will work. – Prathamesh Saraf May 21 '14 at 05:59
  • indeed you are right. thanks for the ehlp. – HurkNburkS May 21 '14 at 21:31

2 Answers2

2

Actually .. I have a little different approach to implement this. Instead of an enum you can directly use a dictionary . This is how you can do it

In you .h you can do this..

#define getDictionaryarray() @[                         \
                                @{                      \
                                    @"Name": @"John",   \
                                    @"ID":@"0"          \
                                },                      \
                                @{                      \
                                    @"Name": @"Jerry",  \
                                    @"ID":@"1"          \
                                    },                  \
                                @{                      \
                                    @"Name": @"Jack",   \
                                    @"ID":@"2"          \
                                },                      \
                                @{                      \
                                    @"Name": @"Jeff",   \
                                    @"ID":@"3"          \
                                },                      \
                                @{                      \
                                    @"Name": @"Jonny",  \
                                    @"ID":@"4"          \
                                },                      \
                            ];

Then the table view datasource methods follow

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *array = getDictionaryarray();
    return array.count;
}

and in -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    NSArray *dictionaryarray = getDictionaryarray();
    cell.textLabel.text = [[dictionaryarray objectAtIndex:indexPath.row] valueForKey:@"Name"];

this would give you a couple of advantages like the id's need not be in the form 0,1,2,3 it can be any number. You can add more attributes to the dictionary like surname etc. And above all it does what you wanted to do :)

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *array = getDictionaryarray();
    NSLog(@"Id for selected Name %@ is %@",[[array objectAtIndex:indexPath.row] valueForKey:@"Name"],[[array objectAtIndex:indexPath.row] valueForKey:@"ID"]);
}

Hope this helps...

Prathamesh Saraf
  • 709
  • 4
  • 14
0

I would approach this in an object-oriented way and define class methods to provide the functions you require -

+(NSString *) stringForName:(Namenum)name
{
  NSString *ret;
  switch (name)
  {
    case Jack:
        ret=@"Jack";
        break;
    case Jonny:
        ret=@"Johnny";
        break;
    ...
  }

  return ret;

}

Paulw11
  • 108,386
  • 14
  • 159
  • 186