当前位置: 编程技术>移动开发
本页文章导读:
▪构建SEL RUNTIME调用步骤 构建SEL RUNTIME调用方法
for (int i = 1; i< 3; i++) { SEL customSelector = NSSelectorFromString([NSString stringWithFormat:@"update%d", i]); if ([self respondsToSelector:customSelector]) { .........
▪ 异步读取raw文件上大文件的内容 异步读取raw文件下大文件的内容
AsyncTask,异步处理,主要用于将比较耗时的操作从主线程(UI线程)中分离出来处理,提高运行速度(流畅度)。本来是要学习AsyncTask的,当看到耗时操作.........
▪ 自定义pickview以及选中成效 自定义pickview以及选中效果
//
// ViewController.m
// UIPickerViewBySelf
//
// Created by MAC on 13-1-29.
// Copyright (c) 2013年 MAC. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementati.........
[1]构建SEL RUNTIME调用步骤
来源: 互联网 发布时间: 2014-02-18
构建SEL RUNTIME调用方法
for (int i = 1; i< 3; i++) {
SEL customSelector = NSSelectorFromString([NSString stringWithFormat:@"update%d", i]);
if ([self respondsToSelector:customSelector]) {
[self performSelector:customSelector];
}
}
[2] 异步读取raw文件上大文件的内容
来源: 互联网 发布时间: 2014-02-18
异步读取raw文件下大文件的内容
AsyncTask,异步处理,主要用于将比较耗时的操作从主线程(UI线程)中分离出来处理,提高运行速度(流畅度)。本来是要学习AsyncTask的,当看到耗时操作时就考虑到了读取大文件操作,然后自己试着读取raw文件夹下面的文件。
有两种方式读取,一种是按行读取,一种是按大小读取;
主要代码如下:
一:按行读取
关键代码:
二:按大小读取
关键代码:
AsyncTask,异步处理,主要用于将比较耗时的操作从主线程(UI线程)中分离出来处理,提高运行速度(流畅度)。本来是要学习AsyncTask的,当看到耗时操作时就考虑到了读取大文件操作,然后自己试着读取raw文件夹下面的文件。
有两种方式读取,一种是按行读取,一种是按大小读取;
主要代码如下:
一:按行读取
关键代码:
class FileReadTask extends AsyncTask<String, String, String> { String line_str; String result; Context context; FileReadTask(Context context) { this.context = context; } int i = 0; @Override protected String doInBackground(String... params) { BufferedReader reader = new BufferedReader(new InputStreamReader( context.getResources().openRawResource(R.raw.test2))); try { while ((line_str = reader.readLine()) != null && !(line_str = reader.readLine()).equals("")) { result += line_str; System.out.println("line_str:"+line_str); publishProgress(line_str); } System.out.println("result:"+result); } catch (IOException e) { e.printStackTrace(); } return line_str; } @Override protected void onPostExecute(String result) { super.onPostExecute(result); } @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected void onProgressUpdate(String... values) { super.onProgressUpdate(values[0]); System.out.println("values:" + values[0]); array.add(values[0]); adapter.notifyDataSetChanged(); } }
二:按大小读取
关键代码:
InputStream input = context.getResources().openRawResource(R.raw.test2); String result = null; int i; byte[] by = new byte[128];//每次读取大小 try { while((i = input.read(by))>0){ byte[] bys = new byte[i]; input.read(bys,0,i); String s = new String(bys); result +=s; System.out.println(s); } } catch (IOException e) { e.printStackTrace(); }
[3] 自定义pickview以及选中成效
来源: 互联网 发布时间: 2014-02-18
自定义pickview以及选中效果
// // ViewController.m // UIPickerViewBySelf // // Created by MAC on 13-1-29. // Copyright (c) 2013年 MAC. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController @synthesize fontlabel; @synthesize pickView; - (void)viewDidLoad { [super viewDidLoad]; self.pickView.dataSource = self; self.pickView.delegate = self; fonts = [UIFont familyNames]; fontSize = [NSArray arrayWithObjects:@"10",@"15", @"20",@"25", @"30",@"35", @"40",@"45", nil]; fontColor =[NSArray arrayWithObjects: [UIColor redColor], [UIColor blueColor], [UIColor blackColor], [UIColor yellowColor], [UIColor grayColor], [UIColor greenColor], [UIColor brownColor], [UIColor orangeColor], [UIColor purpleColor], [UIColor magentaColor],nil]; for(int i= 0;i<3;i++) { int row = 0; if(i==0) { row = [fonts count]/2; } else if (i==1) { row = [fontColor count]/2; } else if (i==2) { row =[fontSize count]/2; } [pickView selectRow:row inComponent:i animated:true]; } // [fontColor retain]; // Do any additional setup after loading the view, typically from a nib. } -(void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ UIView *view0,*view1,*view2; NSInteger row0,row1,row2; row0 = [pickView selectedRowInComponent:0]; row1 = [pickView selectedRowInComponent:1]; row2 = [pickView selectedRowInComponent:2]; view0 = [pickView viewForRow:row0 forComponent:0]; view1 = [pickView viewForRow:row1 forComponent:1]; view2 = [pickView viewForRow:row2 forComponent:2]; UILabel *label1,*label2,*label0; label0 =(UILabel *)[view0 viewWithTag:200]; label1 =(UILabel *)[view1 viewWithTag:200]; label2 =(UILabel *)[view2 viewWithTag:200]; fontlabel.font = [UIFont fontWithName:label0.text size:[label2.text floatValue]]; fontlabel.textColor = label1.backgroundColor; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - #pragma mark UIPICKVIEW CUSTOM FUNTION //返回一共几列的值 -(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 3; } //返回每列具体多少行 -(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { if(component==0) { return [fonts count];} else if(component==1) { return [fontColor count]; } else if(component ==2) { return [fontSize count]; } return -1; } //返回三列各列宽度 -(CGFloat) pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component { if(component==0) { return 180.0f;} else if(component==1) { return 90.0f; } else if(component ==2) { return 50.0f; } return 0.0f; } //返回row高度 -(CGFloat) pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component { return 50.0f; } //可以理解为自定义的view内容 -(UIView *) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { CGFloat width = [self pickerView:pickView widthForComponent:component]; CGFloat rowheight =[self pickerView:pickView rowHeightForComponent:(component)]; UIView *myView = [[UIView alloc]init]; myView.frame =CGRectMake(0.0f, 0.0f, width, rowheight); UILabel *txtlabel = [[UILabel alloc] init]; txtlabel.tag=200; txtlabel.frame = myView.frame; [myView addSubview:txtlabel]; if(component==0) { txtlabel.text = [fonts objectAtIndex:row]; } else if(component==1) { txtlabel.backgroundColor =[fontColor objectAtIndex:row]; } else if(component==2) { txtlabel.text =[fontSize objectAtIndex:row]; } return myView; } @end
最新技术文章: