iOS设置页面项目混乱

时间:2017-11-01 15:13:34

标签: ios objective-c

我继承了一个早在2012年创建的旧iOS项目,并且正在使用一些非常古老的学校技术。我从32位转换为64位。

在设置屏幕上,设置项目的高度不够高,导致设置页面看起来混乱。

知道是什么让这种情况发生了吗?

此页面没有xib文件,它是在代码中生成的。

enter image description here

#import "GeneralSettings.h"

//preference keys
#define kGENERAL_ACCOUNT @"general_account"
#define kGENERAL_EXPAND_RESULTS @"general_expand_results"
#define kGENERAL_FINALS_ONLY @"general_finals_only"
#define kGENERAL_SEARCH_DAYS @"general_search_days"
#define kGENERAL_STARTUP @"general_startup"

@interface GeneralSettings ()

@end

@implementation GeneralSettings

+ (NSString *)accountNumber {
    return [[NSUserDefaults standardUserDefaults] 
objectForKey:kGENERAL_ACCOUNT];
}

+ (BOOL)expandResults {
    NSString *expandResults = [[NSUserDefaults standardUserDefaults] 
objectForKey:kGENERAL_EXPAND_RESULTS];
return expandResults ? [expandResults boolValue] : YES;
}

+ (BOOL)finalsOnly {
    return [[NSUserDefaults standardUserDefaults] 
boolForKey:kGENERAL_FINALS_ONLY];
}

+ (NSInteger)numberOfDays {
    NSString *numberOfDays = [[NSUserDefaults standardUserDefaults] 
objectForKey:kGENERAL_SEARCH_DAYS];

    if (numberOfDays) {
        if (([numberOfDays integerValue] != 90) || [Globals 
sharedInstance].isQA)  {
            return [numberOfDays integerValue];
        }

        //value of 90 lingering from QA session, reset to 15
        [[NSUserDefaults standardUserDefaults] setValue:@"15" 
forKey:kGENERAL_SEARCH_DAYS];

        return 15;
    }

    //default value
    return 7;
}

+ (NSInteger)showAtStartup {
    return [[NSUserDefaults standardUserDefaults] 
integerForKey:kGENERAL_STARTUP];
}

+ (void)setAccountNumber:(NSString *)accountNumber {    
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setValue:accountNumber forKey:kGENERAL_ACCOUNT];
    [prefs synchronize];
}

+ (void)convert {
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

    if ([prefs objectForKey:@"finalsOnly"]) {
        BOOL value = [prefs boolForKey:@"finalsOnly"];

        [prefs removeObjectForKey:@"finalsOnly"];
        [prefs setBool:value forKey:kGENERAL_FINALS_ONLY];
        [prefs synchronize];
    }

    if ([prefs objectForKey:@"searchDays"]) {
        NSString *value = [prefs objectForKey:@"searchDays"];
        int index = 2;

        if ([value isEqualToString:@"1 Days"]) {
            index = 0;
        }
        else if ([value isEqualToString:@"3 Days"]) {
            index = 1;
        }
        else if ([value isEqualToString:@"15 Days"]) {
            index = 3;
        }

        [prefs removeObjectForKey:@"searchDays"];
        [prefs setInteger:index forKey:kGENERAL_SEARCH_DAYS];
        [prefs synchronize];
    }

    if ([prefs objectForKey:@"showAtStartup"]) {
        NSString *value = [prefs objectForKey:@"showAtStartup"];
        int index = 0;

        if ([value isEqualToString:@"Contacts"]) {
            index = 1;
        }
        else if ([value isEqualToString:@"Lookup Test"]) {
            index = 2;
        }

        [prefs removeObjectForKey:@"showAtStartup"];
        [prefs setInteger:index forKey:kGENERAL_STARTUP];
        [prefs synchronize];
    }
}

- (id)init {
    self = [super initWithTitle:xGeneralSettings withIconName:@"icon-
settings-general.png"];

    if (self) {
        //initialization
        Globals *globals = [Globals sharedInstance];
        SettingsSection *section = [self.sections objectAtIndex:0];

        //add account settings section
        SettingsSection *accountSection = [[[SettingsSection alloc] 
initWithTitle:xAccountSettings] autorelease];
        [self.sections addObject:accountSection];

        //create settings
        Setting *startupSetting = [[[Setting alloc] 
initWithKey:kGENERAL_STARTUP withTitle:xGeneralStartupTitle 
withType:pickerSetting] autorelease];
    SearchDaysSetting *searchDaysSetting = [[[SearchDaysSetting alloc] 
initWithKey:kGENERAL_SEARCH_DAYS withTitle:xGeneralSearchDaysTitle 
withType:pickerSetting] autorelease];
        Setting *finalsSetting = [[[Setting alloc] 
initWithKey:kGENERAL_FINALS_ONLY withTitle:xGeneralFinalsOnlyTitle 
withType:toggleSetting] autorelease];
        Setting *expandSetting = [[[Setting alloc] 
initWithKey:kGENERAL_EXPAND_RESULTS 
withTitle:xGeneralExpandResultsTitle withType:toggleSetting] autorelease];
        AccountSetting *accountSetting = [[[AccountSetting alloc] 
initWithKey:kGENERAL_ACCOUNT withTitle:xGeneralAccountTitle 
withType:pickerSetting] autorelease];

        //set setting parameters
        [startupSetting setPickerValuesFromString:xGeneralStartupValues 
withDefaultValue:0];
        [searchDaysSetting 
setPickerValuesFromString:xGeneralSearchDaysValues withDefaultValue:2];
        [finalsSetting setToggle:NO 
withSummary:xGeneralFinalsOnlySummary];
        [expandSetting setToggle:YES 
withSummary:xGeneralExpandResultsSummary];
        [accountSetting setPickerValues:[globals allAccounts] 
withDefaultValue:[globals defaultAccount]];

        if ([Globals sharedInstance].isQA) {
            [searchDaysSetting.pickerValues addObject:@"90 Days"];
        }

        //add settings to sections
        [section.settings addObject:startupSetting];
        [section.settings addObject:searchDaysSetting];
        [section.settings addObject:finalsSetting];
        [section.settings addObject:expandSetting];
        [accountSection.settings addObject:accountSetting];
    }

    return self;
}

@end

#pragma mark - AccountSetting class

@implementation AccountSetting

- (NSInteger)intValue {
    NSArray *picklist = self.pickerValues;
    NSString *value = [self stringValue];

    //return the index of our value
    for (int i = 0; i < picklist.count; i++) {
        if ([[picklist objectAtIndex:i] isEqualToString:value]) {
            return i;
        }
    }

    return [super intValue];
}

- (void)setInteger:(NSInteger)index shouldSave:(BOOL)saveValue {
    NSArray *picklist = self.pickerValues;

    if ((index > -1) && (index < picklist.count)) {
        //save the value determined by this index
        [self setString:[picklist objectAtIndex:index] 
shouldSave:saveValue];
    }
}

@end

#pragma mark - AccountSetting class

@implementation SearchDaysSetting

- (NSInteger)intValue {
    //return the index of our value
    switch ([super intValue]) {
        case 1:
            return 0;
        case 3:
            return 1;
        case 7:
            return 2;
        case 15:
            return 3;
        case 90:
            return [self pickerValues].count - 1; //safe return of assumed index, which might not exist
    }

    return [[self defaultValue] integerValue];
}

- (void)setInteger:(NSInteger)index shouldSave:(BOOL)saveValue {
    NSInteger numberOfDays = 7;

    //save the value determined by this index
    switch (index) {
        case 0:
            numberOfDays = 1;
            break;
        case 1:
            numberOfDays = 3;
            break;
        case 2:
            numberOfDays = 7;
            break;
        case 3:
            numberOfDays = 15;
            break;
        case 4:
            numberOfDays = 90;
            break;
    }

    [super setInteger:numberOfDays shouldSave:saveValue];
}

@end

1 个答案:

答案 0 :(得分:1)

尝试寻找UITableViewDataSource方法:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 46;//here is height of cell
}

或者,在viewDidLoad中添加以下内容:

self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 46;//here is height of cell 
相关问题