您的位置:首页 > 移动开发 > IOS开发

iOS开发基础知识--碎片22

2016-08-22 19:59 375 查看

[b] iOS开发基础知识--碎片22[/b]

[b]1:设置有间距的表格行(UITableViewStyleGrouped)[/b]

1.设置section的数目,即是你有多少个cell
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {    return 3; // in your case, there are 3 cells}
2.对于每个section返回一个cell
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return 1;}
3.设置cell之间headerview的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{    return 10.; // you can have your own choice, of course}
4.设置headerview的颜色
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{    UIView *headerView = [[UIView alloc] init];    headerView.backgroundColor = [UIColor clearColor];    return headerView;}
注意:需要使用 indexpath.section 来获得index,而不是用 indexpath.row
cell.textLabel.text=[NSString stringWithFormat:@"%@",[array objectAtIndex:indexPath.section]];


实例:

创建表格代码:

if (!_myTableView) {
_myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.customheadView.frame), Main_Screen_Width, Main_Screen_Height-204) style:UITableViewStyleGrouped];
_myTableView.backgroundColor = [UIColor clearColor];
_myTableView.showsVerticalScrollIndicator = NO;
_myTableView.showsHorizontalScrollIndicator=NO;
_myTableView.dataSource = self;
_myTableView.delegate = self;
_myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[_myTableView registerClass:[BLSReplenishmentCell class] forCellReuseIdentifier:BLSReplenishmentViewController_CellIdentifier];
[self.view addSubview:_myTableView];
}

其它方法:

#pragma mark UITableViewDataSource和UITableViewDelegate

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

//若设置为0 效果会达不到想要的
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 1;
}

-(NSInteger)numberOfSectionsInTableView:(nonnull UITableView *)tableView
{
return self.recordDatalist.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
BLSReplenishmentCell *cell = [tableView dequeueReusableCellWithIdentifier:BLSReplenishmentViewController_CellIdentifier forIndexPath:indexPath];
cell.cur_Replenishment = [self.recordDatalist objectAtIndex:indexPath.section];
return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return [BLSReplenishmentCell cellHeight];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}


[b]2:Xcode7 使用NSURLSession发送HTTP请求报错[/b]

报错内容:控制台打印:Application Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.

解决办法:修改info.plist文件



[b]3:对UITextField内容实时监听长度和内容[/b]

//第一步,对组件增加监听器
[textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
...
//第二步,实现回调函数
- (void) textFieldDidChange:(id) sender {
UITextField *_field = (UITextField *)sender;
NSLog(@"%@,%d",[_field text],_field.text.length);
}


[b]4:真机调试报[/b]

[b]Please verify that your device's clock is properly set, [/b]

[b]and that your signing certificate is not expired[/b]

注意:在Tagers-build Settings--Code signing--Code Signing Identity 中的Any IOS SDK记得选对证书

[b]5:给UIAlertView增加UITextView,并获得它的值[/b]

MjyAlterView.h

#import <UIKit/UIKit.h>
#import "UIPlaceHolderTextView.h"

typedef void(^AlertViewBlock)(NSInteger index,NSString *textValue);

@interface MjyAlterView : UIAlertView

@property (nonatomic,copy)AlertViewBlock block;

- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles clickButton:(AlertViewBlock)block;

@end

MjyAlterView.m

#import "MjyAlterView.h"

@interface MjyAlterView()<UIAlertViewDelegate,UITextViewDelegate>
@property(copy,nonatomic)NSString *content;
@end

@implementation MjyAlterView

- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message  cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles clickButton:(AlertViewBlock)block{

self = [super initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles, nil];
self.backgroundColor = [UIColor whiteColor];
UIPlaceHolderTextView *textView = [[UIPlaceHolderTextView alloc]init];
textView.delegate=self;
textView.font=[UIFont systemFontOfSize:15];
textView.placeholder=@"输入内容";
textView.layer.borderColor=[UIColor grayColor].CGColor;
textView.layer.borderWidth=0.5;
//    if (SYSTEM_VERSION_LESS_THAN(@"7.0"))//当系统为IOS7时
//    {
//    [testAlert addSubview: textView];
//    }
//    else//当系统为IOS8
//    {
[self setValue: textView forKey:@"accessoryView"];
//    }
if (self) {
_block = block;
}

return self;

}

#pragma mark UIAlertViewDelegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (_block != nil) {
_block(buttonIndex,self.content);
}
}

#pragma mark UITextViewDelegate

- (void)textViewDidChange:(UITextView *)textView{
self.content=textView.text;
}

@end


调用:
__weak ViewController *weakThis = self;
AlertViewBlock block  = ^(NSInteger index,NSString *content) {
__strong ViewController *strongThis = weakThis;
if (index == 1) {
NSLog(@"确定,--%@",content);
}else if (index == 0){

strongThis.showLabel.text = @"取消";
}
};

MjyAlterView *alterView = [[MjyAlterView alloc] initWithTitle:@""message:@""cancelButtonTitle:nil otherButtonTitles:@"确定" clickButton:block];

[alterView show];


[b] 6:iOS UILabel显示HTML文本(IOS7以上)[/b]


NSString * htmlString = @"<html><body> Some html string \n <font size=\"13\" color=\"red\">This is some text!</font> </body></html>";
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
UILabel * myLabel = [[UILabel alloc] initWithFrame:self.view.bounds];
myLabel.attributedText = attrStr;
[self.view addSubview:myLabel];


运用实例(自动高度)

if (self.contentLabel==nil) {
self.contentLabel=[[UILabel alloc]init];
self.contentLabel.textColor=COLOR_WORD_GRAY_1;
self.contentLabel.font=[UIFont systemFontOfSize:14];
self.contentLabel.numberOfLines=0;
[self.contentLabel sizeToFit];
[self.contentView addSubview:self.contentLabel];
[self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.contentView.left).with.offset(leftSpace);
make.right.mas_equalTo(self.contentView.right).with.offset(-leftSpace);
make.top.mas_equalTo(self.lineView.bottom).with.offset(topSpace);
}];
}

赋值:
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[model.content dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
self.contentLabel.attributedText = attrStr;


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: