各个组件的简单学习:
slider的用法:
slider关联value change 事件
比如改变屏幕的背景色
UISlider *slider = (UISlider*)sender;
float v = slider.value + 0.2f;
[self.view setBackgroundColor:[[UIColor alloc] initWithRed:0.2+v green:0.5+v blue:v alpha:0.8]];
webview的用法
webview用来加载网页内容
NSURL *url = [NSURL URLWithString:@"http://www.cnblogs.com/zhuqil/archive/2011/07/28/2119923.html"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webview loadRequest:request];
如果需要知道加载状态则需要实现UIWebviewDelegate协议
在使用的时候需要[self.webview setDelegate:self];设置该协议的代理,之前由于没有设置,导致以下几个方法没有运行:
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
}
-(void)webViewDidStartLoad:(UIWebView *)webView{
self.acIndicator.hidden = NO; //UIActivityIndicatorView的使用,属性可见与不可见
[self.acIndicator startAnimating];//UIActivityIndicatorView的动画,start启动动画则转,stop为停止动画
NSLog(@"startload");
}
-(void)webViewDidFinishLoad:(UIWebView *)webView{
[self.acIndicator stopAnimating];
self.acIndicator.hidden = YES;
NSLog(@"finishload");
}
tableview的使用
最简单的使用,接口继承UITableViewDelegate和UITableViewDataSource两个协议
在viewDidLoad方法设置代理和数据源,
至少需要实现的方法是:
//返回这个section的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [arr count];
}
//定制每行怎么显示
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString * identifier = @"tableviewIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [arr objectAtIndex:row];
return cell;
}
数据用NSMutableArray就可以了
自定义TableViewCell的学习
创建一个Objective-C文件继承自UITableViewCell
再创建一个empty xib文件,向里面拖入UITableViewCell 和要显示的元素,并将xib与.h文件关联
然后在
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString * identifier = @"tableviewIdentifier";
static BOOL nibRegistered = NO;
if(!nibRegistered){
UINib *nib = [UINib nibWithNibName:@"CustomCell" bundle:nil];
[tableView registerNib:nib forCellReuseIdentifier:identifier];
nibRegistered = YES;
}
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
NSInteger row = [indexPath row];
NSDictionary *dict = [dataList objectAtIndex:row];
cell.name = [dict objectForKey:@"name"];
cell.dec = [dict objectForKey:@"dec"];
cell.loc = [dict objectForKey:@"loc"];
cell.image = [imageList objectAtIndex:row];
return cell
}
又犯了一个低级错误
NSMutableArray 没有初始化就往里面addObject最后在列表中查不到数据,太初心大意了!