如何在单元格内容视图和分隔符之间增加间距?

时间:2018-07-14 17:32:02

标签: objective-c uitableview

我正在尝试实现以下所示的设计,但是占位符为“输入您的显示名称”的UITextField过于靠近分隔符。我试过根据节和行更改cellHeightForIndex,但是没有达到预期的效果。如何在内容视图和分隔符之间添加间距?

Current implementation

Desired implementation

#define kCellSeparatorTag 100


@interface EditProfileInfoViewController ()<UITableViewDataSource ,UITableViewDelegate, UIImagePickerControllerDelegate, UITextFieldDelegate>
@property (nonatomic, strong) NSArray *editPersonalInfoTitles;
//@property (strong, nonatomic) IBOutlet UIButton *userProfilePhotoButton;
@property (strong, nonatomic) IBOutlet UILabel *fullNameLabel;
@property (strong, nonatomic) IBOutlet UIButton *profilePictureButton;

@property (strong, nonatomic) IBOutlet UILabel *addPhotoLabel;

@property (nonatomic, strong) IBOutlet UITableView *tableView;
@property (nonatomic, strong) UITextField *displayNameTextField;

@end

@implementation EditProfileInfoViewController

NSMutableString *usersFirstNameString;
NSMutableString *usersLastNameString;
NSDate *usersDateOfBirth;
NSMutableString *usersWebsiteString;


+ (NSString*)storyboardID{
    return @"EditProfileInfo";
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:true];
    [self downloadUserRoleToDisplayMenu];

}

-(void)downloadUserRoleToDisplayMenu{
    [NetworkManager callURL:@"http://baseapis.net" withDict:nil method:@"GET" JSON:YES success:^(id  _Nonnull responseObject) {

        NSLog(@"%@", responseObject);

        usersFirstNameString = responseObject[@"data"][0][@"firstname"];
        usersLastNameString = responseObject[@"data"][0][@"lastname"];


        NSLog(@"We're on the Edit Profile Info screen now and the imageURL is: %@", [CMUser currentUser][@"imageUrl"]);

        [self.profilePictureButton sd_setImageWithURL:[NSURL URLWithString:[CMUser currentUser][@"imageUrl"]]forState:UIControlStateNormal];

        if ([self.profilePictureButton imageForState:(UIControlStateNormal) != nil]) {
            NSLog(@"The imageForState UIControlStateNormal is not nil so it shows an image");
            self.addPhotoLabel.text = @"";
        }

        //TODO: Implement User Date of Birth on the Database and parse responseObject for the DOB to place inside usersDateOfBirth variable.

        //TODO: Implement User Website address listing on Personal Info.  Parse responseObject for the users website address to place inside usersWebsiteString variable.
    } failure:^(id  _Nonnull responseObject, NSError * _Nonnull error) {
        NSLog(@"%@", error);
    }];

}

- (void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    CGFloat revealWidth = ApplicationDelegate.drawerController.maximumLeftDrawerWidth;
    CGRect frame = self.view.frame;
    frame.size.width = revealWidth;
    self.view.frame = frame;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.profilePictureButton.layer.cornerRadius = (revealWidth / 3) / 2;

}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationBarHidden = YES;
    [self downloadUserRoleToDisplayMenu];
    self.profilePictureButton.imageView.contentMode = UIViewContentModeScaleToFill;
    self.profilePictureButton.imageView.clipsToBounds = YES;
    self.profilePictureButton.clipsToBounds = YES;
    [self.profilePictureButton addBorder:[UIColor colorWithHex:0x666666] width:1];
    self.editPersonalInfoTitles = @[@[@"Full Name", @"Display Name", @"DOB", @"Website URL", @"Facebook URL", @"Linkedin"]];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"PersonalInfoCell"];
    self.tableView.backgroundColor = [UIColor colorWithHex:0x222222];
    //beginning of full name field
    if (usersFirstNameString == nil || usersLastNameString == nil){
        self.fullNameLabel.text = [NSString stringWithFormat:@"Full name not available"];

    } else {
        self.fullNameLabel.text = [NSString stringWithFormat:@"%@ %@", usersFirstNameString, usersLastNameString];
    }
    //end of full name field
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return self.editPersonalInfoTitles.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    NSArray *menuItems = self.editPersonalInfoTitles[section];
    return menuItems.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.0;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *v = [UIView new];
    [v setBackgroundColor:[UIColor clearColor]];
    return v;
}

- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70.0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *menuItems = self.editPersonalInfoTitles[indexPath.section];
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"PersonalInfoCell"];
    cell.contentView.backgroundColor = [UIColor clearColor];
    cell.backgroundColor = [UIColor clearColor];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.text = menuItems[indexPath.row];

    //start of separating user data info
    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 0:
            {
                cell.accessoryType = UITableViewCellAccessoryNone;
                NSLog(@"Profile Info tapped");
                NSString *dateOfBirthString = [[NSString alloc] init];
                NSString *websiteString = [[NSString alloc] init];
                //beginning of full name field
                if (usersFirstNameString == nil || usersLastNameString == nil){
                    cell.detailTextLabel.text = [NSString stringWithFormat:@"Full name not available"];

                } else {
                    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ %@", usersFirstNameString, usersLastNameString];
                }
                //end of full name field
                break;
            }
            default:
                break;
        }
        switch (indexPath.row) {
            case 1:{
                cell.accessoryType = UITableViewCellAccessoryNone;
                self.displayNameTextField = [[UITextField alloc] initWithFrame:CGRectMake(15, 40, 375, 30)];
                UIColor *color = [UIColor whiteColor];
                self.displayNameTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Enter your display name" attributes:@{NSForegroundColorAttributeName: color, NSFontAttributeName : [UIFont fontWithName:@"OpenSans" size:14]}];
                [self.displayNameTextField setTextColor:[UIColor whiteColor]];
                self.displayNameTextField.autocorrectionType = UITextAutocorrectionTypeNo;
                [self.displayNameTextField setClearButtonMode:UITextFieldViewModeWhileEditing];
                //[cell addSubview:self.displayNameTextField];
                [cell.contentView addSubview:self.displayNameTextField];

                break;
            }
            default:
                break;
        }
        switch (indexPath.row) {
            case 2:{
                cell.accessoryType = UITableViewCellAccessoryNone;
                cell.detailTextLabel.numberOfLines = 1;
                cell.detailTextLabel.text = @"Date of birth not available";
                break;
            default:
                break;
            }
        }
        switch (indexPath.row) {
            case 3:{
                cell.detailTextLabel.numberOfLines = 2;
                cell.detailTextLabel.text = @"www.domain.com";
                break;
            default:
                break;
            }
        }
        switch (indexPath.row) {
            case 4:{
                cell.detailTextLabel.numberOfLines = 2;
                cell.detailTextLabel.text = @"www.facebook.com/your-name";
                break;
            default:
                break;
            }
        }
        switch (indexPath.row) {
            case 5:{
                cell.detailTextLabel.numberOfLines = 2;
                cell.detailTextLabel.text = @"www.linkedin.com/your-name";

                break;
            default:
                break;
            }
        }
        //end of separating user data info
        cell.detailTextLabel.textColor = [UIColor whiteColor];
        cell.detailTextLabel.font = [UIFont fontWithName:@"OpenSans" size:14];
        cell.textLabel.textColor = [UIColor colorWithHex:0xc0c0c0];
        cell.textLabel.font = [UIFont fontWithName:@"OpenSans" size:12];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;



        UIView *separatorView = [cell.contentView viewWithTag:kCellSeparatorTag];
        if (!separatorView) {
            CGFloat revealWidth = ApplicationDelegate.drawerController.maximumLeftDrawerWidth;
            separatorView = [[UIView alloc] initWithFrame:CGRectMake(0, 63, revealWidth, 1)];
            separatorView.backgroundColor = [UIColor grayColor];
            [cell.contentView addSubview:separatorView];
        }else{

        }
    }
    self.displayNameTextField.delegate = self;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath
                             animated:YES];
    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 0:
                NSLog(@"Profile Info tapped");
            default:
                break;
        }
        switch (indexPath.row) {
            case 1:
                NSLog(@"Email tapped");
                //[self EmailTapped];
            default:
                break;
        }
        switch (indexPath.row) {
            case 2:
                NSLog(@"Phone number tapped");
                // [self PhoneNumberTapped];
                break;
            default:
                break;
        }
        switch (indexPath.row) {
            case 3:
                NSLog(@"Address tapped");
                //[self AddressTapped];
                break;

            default:
                break;
        }
        switch (indexPath.row) {
            case 4:
                NSLog(@"Account Manager tapped");
                //[self AccountManagerTapped];
                break;

            default:
                break;
        }
        switch (indexPath.row) {
            case 5:
                NSLog(@"Account Manager tapped");
                //[self AccountManagerTapped];
                break;

            default:
                break;
        }
    }
}


- (IBAction)backButtonTapped{
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)logoutTapped{
    [ApplicationDelegate toggleMenu];
    [CMUser logOut];
    [ApplicationDelegate setController];
}

- (IBAction)addPhotoButtonTapped:(UIButton *)sender {
    [self configureAddPhotoActionSheet];
}

- (void)takePhoto{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;

    [self presentViewController:picker animated:YES completion:NULL];
}

- (void)selectPhoto{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    [self presentViewController:picker animated:YES completion:NULL];
}

-(void)configureAddPhotoActionSheet{
    UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];

    [actionSheet addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {

        // Cancel button tappped do nothing.

    }]];

    [actionSheet addAction:[UIAlertAction actionWithTitle:@"Take a photo" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

        // take photo button tapped.
        [self takePhoto];

    }]];

    [actionSheet addAction:[UIAlertAction actionWithTitle:@"Choose photo from Library" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

        // choose photo button tapped.
        [self selectPhoto];

    }]];


    [self presentViewController:actionSheet animated:YES completion:nil];
}


//UIImagePickerDelegate Methods
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];

    [self.profilePictureButton setImage:chosenImage forState:UIControlStateNormal];
    self.addPhotoLabel.text = @"";
    [self updateProfilePicture:chosenImage];
    [picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

    [picker dismissViewControllerAnimated:YES completion:NULL];
}

- (void)updateProfilePicture:(UIImage*)image{
    NSData *imageData = UIImageJPEGRepresentation(image, 0.5);

    NSString *uploadEndPoint = @"artist/add-artist-photo";
    MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.profilePictureButton animated:YES];
    HUD.mode = MBProgressHUDModeAnnularDeterminate;
    [NetworkManager uploadToEndPoint:uploadEndPoint
                          parameters:@{@"imageFile" : @"image",
                                       @"image" : @"image"}
                                data:imageData
                            fileName:@"profile"
                              isJSON:NO
                         uploadBlock:^(NSProgress *uploadProgress) {
                             HUD.progressObject = uploadProgress;
                         } downloadBlock:^(NSProgress *downloadProgress) {

                         } success:^(id responseObject) {
                             [HUD hideAnimated:YES];
                         } failure:^(id responseObject, NSError *error) {
                             [HUD hideAnimated:YES];
                         }];
}




@end

1 个答案:

答案 0 :(得分:0)

该问题可能是因为您没有在单元格上设置detailTextLabel属性。尝试将其设置为空格字符并向上移动子视图。

在您的case 1中添加:

cell.detailTextLabel.text = @" ";

还可以稍微改变子视图的位置(y从40更改为20):

self.displayNameTextField = [[UITextField alloc] initWithFrame:CGRectMake(15, 20, 375, 30)];

我注意到的另一件事是您以非标准方式使用switch语句。而不是这样做:

switch (expression) {
    case n:
        statements;
        break;
    default:
        break;
}
switch (expression) {
    case n+1:
        statements;
        break;
    default:
        break;
}

这样做:

switch (expression) {
    case n:
        statements;
        break;
    case n+1:
        statements;
        break;
    case n+2:
        statements;
        break;
    default:
        break;
}