下一个控制器有xib的时候:
GoodsDetailsViewController *gdVC = [[GoodsDetailsViewController alloc]initWithNibName:@"GoodsDetailsViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:gdVC animated:YES];
推出的是tableview的cell有xib的时候:
DIYSelectHousingTypeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DIYSelectHousingTypeCell"];
if (!cell) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"DIYSelectHousingTypeCell" owner:self options:nil] firstObject];
}
加载一个xib格式的view
OrderStateView *stateView = [[[NSBundle mainBundle]loadNibNamed:@"OrderStateView" owner:self options:nil] firstObject];
手动添加约束:
[bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(@0);
make.left.equalTo(@0);
make.right.equalTo(@(Width));
make.height.equalTo(@40);
// make.height.equalTo(self.scrollerView.mas_height);
}];
pragma mark - refresh
(void)loadMJTableViewFooter
{
MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
[footer setTitle:@"" forState:MJRefreshStateIdle];
[footer setTitle:@"正在加载更多" forState:MJRefreshStateRefreshing];
[footer setTitle:@"已经全部显示了" forState:MJRefreshStateNoMoreData];
footer.stateLabel.font = [UIFont systemFontOfSize:15];
footer.stateLabel.textColor = [UIColor grayColor];
_tableView.footer = footer;
}(void)loadMoreData
{
[self downloadDataWithIsRefresh:YES];
}
//如果 想让某个方向上不能滚动 那么这个方向对应的尺寸数值传0
// self.contentSize = CGSizeMake(4*_scrollView.width, 0);
//头视图上按钮的点击事件
-(void)groupChooseClick:(UIButton *)sender
{
sender.selected = !sender.selected;//让组视图上的按钮的选择状态为相反的状态
CartGroupItem * groupItem = self.dataSources[sender.tag];//获取数据源中的这个组
groupItem.groupIsSelected = sender.selected;//将这个组里面的selected属性设置成按钮的属性
for(CartItem * cart in groupItem.cartItems)//遍历这个组里面所有的数据,将其的选择状态设置成按钮的选择状态
{
cart.goodsSelected = sender.selected;
//这是将其加入到购买的数组 如果按钮的选择状态为YES的话
if(sender.selected == YES)
{
//如果将要购买的数组里面没有数据,就将数据插入到里面
if(self.payCartItems.count ==0)
{
[self.payCartItems addObject:cart];
}
//否则的话就判断里面有没有同一个数据,如果有先将其删除再添加 以保证没有重复的数据
else
{
for (int i =0; i < self.payCartItems.count; I++)
{
if([cart isEqual:self.payCartItems[I]])
{
[self.payCartItems removeObject:cart];
}
}
[self.payCartItems addObject:cart];
}
}
else
{
[self.payCartItems removeObject:cart];
}
}
[self calculateAllCartsPrice];//这是计算所有的商品的总价
//定义一个局部变量 为全选按钮的选择状态
BOOL isSelected = YES;
for (CartGroupItem * groupItem in self.dataSources)//遍历数据源中所有组的选择状态,如果全都是YES将其置成YES 否则将其置成NO
{
if(groupItem.groupIsSelected == NO)
{
isSelected = NO;
break;
}
isSelected = YES;
}
//这里的 self.selectedAllBtn 是全局的全选按钮
self.selectedAllBtn.selected = isSelected;//设置全选按钮的选择状态
[self showNavigationTitle];//这是显示navagationBar上title的方法
[self.goodsTableView reloadData];//刷新TableView
}
跳转:
SensorsDataViewController *vc = [[SensorsDataViewController alloc]initWithNibName:@"SensorsDataViewController" bundle:[NSBundle mainBundle]];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
nav.navigationBar.translucent = NO;
vc.itemNum = indexPath.row;
[self presentViewController:nav animated:YES completion:^{
}];
本地读取json:
NSString *jsonPath = [[NSBundle mainBundle] pathForResource:@"detectioncenter" ofType:@"js"];
NSData *data=[NSData dataWithContentsOfFile:jsonPath];
NSError *error;
id jsonObject=[NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingAllowFragments
error:&error];
NSMutableArray *array = [jsonObject valueForKey:@"result"];
for (NSDictionary *dic in array) {
FarmlandModel *model = [FarmlandModel new];
[model setValuesForKeysWithDictionary:dic];
[self.array addObject:model];
}
[_tableView reloadData];
黑色半透明背景遮罩
self.blackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Width, Height)];
self.blackView.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.5];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(blackViewTap)];
[self.blackView addGestureRecognizer:tap];
}
解决webview滑动卡顿的问题
self.webView.scrollView.decelerationRate = UIScrollViewDecelerationRateNormal;
返回结果打成plist文件 NSFileManager *fileManager = [NSFileManager defaultManager];NSURL *documentPath = [fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask][0] ;NSURL *filePath = [documentPath URLByAppendingPathComponent:@"datasource.plist"];[resultArray writeToURL:filePath atomically:YES];NSLog(@"文件存储路径%@", filePath);
设置网络图片
NSString *picString = [NSString stringWithFormat:@"%@/cjh/picture/merchant/%@",URL,[shopArray[indexPath.row] valueForKey:@"HEAD_IMAGE"]];
NSURL picURL = [NSURL URLWithString:picString];
[cell.goodsImageView sd_setImageWithURL:picURL];
//dictionary转化成nsstring
-(NSString)DataTOjsonString:(id)object
{
NSString *jsonString = nil;
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:object
options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
error:&error];
if (! jsonData) {
NSLog(@"Got an error: %@", error);
} else {
jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
return jsonString;
}
拨打电话
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"telprompt://%@", self.phoneString]] options:@{} completionHandler:^(BOOL success) {
}];
拿到cell里面某个button的indexPath
ShoppingCarContentCell *cell = (ShoppingCarContentCell *)button.superview.superview;
NSIndexPath *indexPath = [self.tabView indexPathForCell:cell];
IQKeyBoard 对于searchBar不可用解决办法:
导航栏右侧设置两个按钮
UIButton *contactButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[contactButton setImage:[UIImage imageNamed:@"service_black"] forState:UIControlStateNormal];
[contactButton addTarget:self action:@selector(contact:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftBtnItem = [[UIBarButtonItem alloc] initWithCustomView:contactButton];
UIButton *informationButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
[informationButton setImage:[UIImage imageNamed:@"mine_message"] forState:UIControlStateNormal];
[informationButton addTarget:self action:@selector(information:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightBtnItem = [[UIBarButtonItem alloc]initWithCustomView:informationButton];
self.navigationItem.rightBarButtonItems = @[rightBtnItem,leftBtnItem];
返回上两页
NSInteger index = [[self.navigationController viewControllers] indexOfObject:self];
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:index - 2]animated:YES];
导航条右侧按钮
UIButton *submitButton = [UIButton buttonWithType:UIButtonTypeCustom];
submitButton.frame = CGRectMake(0, 0, 35, 35);
[submitButton setTitleColor:colorWithMainColor forState:UIControlStateNormal];
[submitButton setTitle:@"提交" forState:normal];
submitButton.titleLabel.font = [UIFont systemFontOfSize:15.0];
[submitButton addTarget:self action:@selector(submitInfo:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:submitButton];
self.navigationItem.rightBarButtonItem = rightButtonItem;
fd防止崩溃
self.nameLabel.preferredMaxLayoutWidth = YGScreenWidth - 30; //fd适配高度 numberOfLines为0的label加上这句话 等于的宽度是实际的宽度 如果不加这句话在手机是10.2的时候会崩溃
iOS字符串中包含%
百分号的转换,NSString中需要格式化的字符串中百分号使用%%表示,而char*中百分号也是使用%%表示。
例如:NSLog(@"%%%@%%",@"hello"),控制台会打印出%hello%。