【IOS-UIKit学习】模仿设置界面

一、描述

1.需求分析
1)原始界面
在这里插入图片描述
2)组件分析

I.整体分析:看构造主体为tableview因此采用tableview设置此页面

II.部件分析

A.头部:”设置“标题
采用UIView+UILabel(text属性设置为"设置"),并将其放在tableview

B.searchBar
采用searchBar,需要设置holderplace为搜索和旁边的取消按钮

C.Cell
三个section(段),分别含有1,4,5个row(行),不同行需要根据需求设置图标以及">"、子标题和开关组件。

a.设置">":

cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;

b.设置"开关组件"

     UISwitch *switchview = [[UISwitch alloc] initWithFrame:CGRectZero];
                // 设置右边的组件
                cell.accessoryView = switchview;

c.设置子标题(设置为value1的cell 使用detailText进行设置)

d.设置图标(设置cell的imageView)
图标
3)效果
在这里插入图片描述
图标下载:https://mp.csdn.net/console/upDetailed?utm_medium=fe.tool_bar_second.download.my_resources

二、代码

//
//  TableViewController.m
//  Demo
//
//  Created by 吴伟 on 2020/9/22.
//

#import "TableViewController.h"

@interface TableViewController ()

@end

@implementation TableViewController

// 设置整体组件
- (void)viewDidLoad {
    [super viewDidLoad];
    // 附加组件才需要在这儿引入??
    
    // 调用header
    self.tableView.tableHeaderView=[self getTableHeaderView];

    // 调用searchBar
    [self setSearchBar];
    
}

- (void)setSearchBar{
    
    UISearchBar *searchBar=[[UISearchBar alloc]initWithFrame:CGRectMake(10, 68, 300, 28)];
    
        searchBar.placeholder=@"搜索";

    // 去背景色
    searchBar.searchBarStyle=UISearchBarStyleMinimal;
    // 显示取消按钮
    [searchBar setShowsCancelButton:YES animated:YES];
    // 添加进此view中
    [self.view addSubview:searchBar];
}

// 设置点击事件
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    [searchBar resignFirstResponder]; //searchBar失去焦点
       UIButton *cancelBtn = [searchBar valueForKey:@"cancelButton"]; //首先取出cancelBtn
       cancelBtn.enabled = YES; //把enabled设置为yes
}

// 设置头部(View+Label)
- (UIView*)getTableHeaderView{
    UIView* vc=[[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 110)];
    UILabel* label=[[UILabel alloc]initWithFrame:CGRectMake(20, 30, [UIScreen mainScreen].bounds.size.width, 40)];
    label.font=[UIFont boldSystemFontOfSize:26];
    label.text=@"设置";
    label.textColor=[UIColor blackColor];
    [vc addSubview:label];
    return vc;
}

#pragma mark - Table view data source
// 设置段数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}
// section:段、row:行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (section) {
        case 0:
            section = 1;
            break;
        case 1:
            section = 5;
            break;
        case 2:
            section=4;
            break;
        default:
            break;
    }
    return section;
}
// cellForRowIndexPath 设置section、row中的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"%ld,%ld",(long)indexPath.section,(long)indexPath.row] ];
    
    int types[]={
        UITableViewCellStyleSubtitle
        ,UITableViewCellStyleValue1
        ,UITableViewCellStyleValue2
    };
    
    NSString *text1
    []=
    {
         @"飞行模式"
        ,@"无线局域网"
        ,@"蓝牙"
        ,@"窝峰网络"
        ,@"个人热点"
    };
    NSString *text2
    []=
    {
         @"通知"
        ,@"声音与触感"
        ,@"勿扰模式"
        ,@"屏幕使用时间"
    };
    
    NSString *icon1
    []=
    {
        @"飞行模式"
       ,@"无限局域网"
       ,@"蓝牙"
       ,@"蜂窝数据"
       ,@"热点"
    };
    NSString *icon2
    []=
    {
        @"通知"
       ,@"声音"
       ,@"勿扰模式"
       ,@"沙漏倒计时"
    };

    if(cell==nil){
        // 使用initWithCell进行row创建(三种形式)
        cell=[[UITableViewCell alloc]initWithStyle:types[indexPath.section] reuseIdentifier:[NSString stringWithFormat:@"%ld,%ld",(long)indexPath.section,(long)indexPath.row]];
        
        if(indexPath.section == 0){
            [cell initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cellID"];
            // 设置标题
            cell.textLabel.text = @"吴伟";
            // 设置子标题
            cell.detailTextLabel.text = @"Apple ID、iCloud、媒体与购买项目";
            // 设置imageView
            [cell.imageView setImage:[UIImage imageNamed:@"Icon"]];
            // 设置圆角头像
            cell.imageView.layer.masksToBounds = YES;

            cell.imageView.layer.cornerRadius = 20;
            // 设置右边箭头
            cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
                }

        if(indexPath.section == 1){
           
            [cell initWithStyle:types[1] reuseIdentifier:[NSString stringWithFormat:@"%ld,%ld",(long)indexPath.section,(long)indexPath.row]];
            
            cell.textLabel.text = text1[indexPath.row];
            
            [cell.imageView setImage:[UIImage imageNamed:icon1[indexPath.row]]];
          
            if(indexPath.row==0){
                
                UISwitch *switchview = [[UISwitch alloc] initWithFrame:CGRectZero];
                // 设置右边的组件
                cell.accessoryView = switchview;
//
            }else{
                
                NSString *title
                []={
                     @"未连接"
                    ,@"打开"
                    ,@""
                    ,@"关闭"
                };
                
                cell.detailTextLabel.text=title[indexPath.row];
                cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
            }
        }
        
        if(indexPath.section == 2){
            
            [cell initWithStyle:types[1] reuseIdentifier:[NSString stringWithFormat:@"%ld,%ld",(long)indexPath.section,(long)indexPath.row]];
            
            
            cell.textLabel.text = text2[indexPath.row];
                    [cell.imageView setImage:[UIImage imageNamed:icon2[indexPath.row]]];
        
            cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
          }
        }
        
    if(indexPath.section==0){
        CGSize itemSize = CGSizeMake(40, 40);//希望显示的大小
           UIGraphicsBeginImageContextWithOptions(itemSize, NO, UIScreen.mainScreen.scale);
           CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
           [cell.imageView.image drawInRect:imageRect];
           cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
           UIGraphicsEndImageContext();
    }else{
        
        CGSize itemSize = CGSizeMake(25, 25);//希望显示的大小
           UIGraphicsBeginImageContextWithOptions(itemSize, NO, UIScreen.mainScreen.scale);
           CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
           [cell.imageView.image drawInRect:imageRect];
           cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
           UIGraphicsEndImageContext();
    }
           
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UIAlertController* alert=[UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"我是第%ld个section中的第%ld个cell",indexPath.section,(long)indexPath.row] message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction* okAction=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    [alert addAction:okAction];
    [self presentViewController:alert animated:YES completion:nil];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值