如何正确使用 typedef NS_ENUM



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

所以我看到一些苹果示例代码,帮助我或多或少地理解应该如何做到这一点,并且由于某种原因它不起作用。

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

#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也没有被召唤......为什么?

谢谢!!

您应该设置表视图的委托。

tableView.delegate = self;

确保表视图的委托已得到重视。

那么,有多少节呢?

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

最新更新