一、描述
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