如何正确使用typedef NS_ENUM?

时间:2015-01-04 10:04:44

标签: ios objective-c uitableview typedef

我想使用typedef NS_ENUM在我的didSelectRowAtIndexPath方法中创建一个切换方法,以便为不同类型的单元格选择创建操作。

所以我看到了一些苹果示例代码,这些代码帮助我或多或少地了解了应该如何完成这些代码,并且由于某些原因它无法正常工作。

这是我在视图控制器中的相关方法:

#import "SettingsTableViewController.h"
#import <MessageUI/MessageUI.h>

// Corresponds to the section index of the table view
typedef NS_ENUM(NSInteger, SettingsControllerSection) {
    SettingsControllerSectionCell = 0,
    OtherControllerSection
};

// Corresponds to the row in the first section.
typedef NS_ENUM(NSInteger, SettingsControllerWebsitesSection) {
    FirstWebsiteController = 0,
    SecondWebsiteController,
    ThirdWebsiteController,
    FourthWebsiteController,
    FithWebsiteController,
    SixthWebsiteController,
    SeventhWebsiteController
};

// Corresponds to the row in the second section.
typedef NS_ENUM(NSInteger, SettingsControllerOtherSection) {
    RateUsController = 0,
    ContactUsControllerRow
};


@interface SettingsTableViewController ()<MFMailComposeViewControllerDelegate>

@end

@implementation SettingsTableViewController

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    //Settings up the sections title
    if(section == 0) {

        return @"News Sources";
    } else {
        return @"Share & Care";
    }
}

- (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];
    }
    //getting the cell value
    NSDictionary *dictionary = [self.dataArray objectAtIndex:indexPath.section];
    NSArray *array = [dictionary objectForKey:@"data"];
    NSString *cellValue = [array objectAtIndex:indexPath.row];

    cell.textLabel.text = cellValue;
    //adding switch objects to the news sources section
    if (indexPath.section == 0) {
        UISwitch *switchview = [[UISwitch alloc] initWithFrame:CGRectZero];
        //switchview.thumbTintColor = [UIColor colorWithRed:14.0/255.0 green:118.0/255.0 blue:127.0/255.0 alpha:1];
        [switchview setOnTintColor:[UIColor colorWithRed:14.0/255.0 green:118.0/255.0 blue:127.0/255.0 alpha:1]];
        cell.accessoryView = switchview;
    }

    return cell;
}

- (void)sendEmail {
    // Email Subject
    NSString *emailTitle = @"Test Email";
    // Email Content
    NSString *messageBody = @"iOS programming is so fun!";
    // To address
    NSArray *toRecipents = [NSArray arrayWithObject:@"support@appcoda.com"];

    MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
    mc.mailComposeDelegate = self;
    [mc setSubject:emailTitle];
    [mc setMessageBody:messageBody isHTML:NO];
    [mc setToRecipients:toRecipents];

    // Present mail view controller on screen
    [self presentViewController:mc animated:YES completion:NULL];

}

- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Mail cancelled");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Mail saved");
            break;
        case MFMailComposeResultSent:
            NSLog(@"Mail sent");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Mail sent failure: %@", [error localizedDescription]);
            break;
        default:
            break;
    }

    // Close the Mail Interface
    [self dismissViewControllerAnimated:YES completion:NULL];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    SettingsControllerSection section = indexPath.section;

    if (OtherControllerSection == section) {
        SettingsControllerOtherSection row = indexPath.row;

        switch (row) {

            case RateUsController:
                NSLog(@"rate us was pressed");
                break;

            case ContactUsControllerRow:
                [self sendEmail];
                NSLog(@"send email was pressed");
                break;
        }
    }
}

现在我看到我的didSelectRowAtIndexPath也没有被调用....为什么会这样?

谢谢!

1 个答案:

答案 0 :(得分:0)

您应该设置tableview的委托。

tableView.delegate = self;

确保您的tableview的委托已被重视。

然后,有多少部分?

NS_ENUM只是枚举的另一个声明。