UITableView这个控件可以说是最常用的控件了,由于刚开始学习IOS找了好多资料,才搞明白他的基本用法:
UITableView本身要比UIButton 、UILabel等控件要稍微复杂一点
在这里是拖的控件,也就是UITableView控件。
1.如果是拖得控件,那么就要把dataSource和delegate 通过File's owner 使他们之间能够进行想联系,也就是要她们通过拖拽与File's owner进行连接.
2.要定义自己的类:@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
@property (strong,nonatomic)NSArray *list;
@end
3.其他的实现方式都是在.m文件中实现的:(1).例如:在-(void)ViewDidLoad{
}函数里实现添加数据.
(2).在-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.list count];
}想这个函数,是用来返回你添加的数据的个数,也就是对象的个数。
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
在里面你可以实现层的复用等例如:
static NSString *str=@"Sameide";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:str];//在这里就是实现复用方法.
if(cell==nil)
{
cell=[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:str];
}
NSUInteger row=[indexPath row];
cell.textLabel.text=[self.list objectAtIndex:row];
UIImage *image=[UIImage imageNamed:@"123.jpg"];
cell.imageView.image=image;
UIImage *hig=[UIImage imageNamed:@"youdao"];
cell.imageView.highlightedImage=hig;
cell.detailTextLabel.text=@"hello";
return cell;
}
这个函数的作用就是用来返回每个cell的数据,要注意index的用法以及他包含的内容.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *str=[self.list objectAtIndex:[indexPath row]];
UIAlertView *alter=[[UIAlertView alloc] initWithTitle:@"选中行信息" message:str delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alter show];
}在这个函数里实现的是cell上的点击事件。当你点击cell的时候会显示一些相关的信息.
当然这里也可以用代码实现,今天就先到这里了,希望对你们有帮助.