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

ios tableView那些事(三)给tableView添加些图片

2015-04-26 00:26 375 查看
感觉光有数据的tableView很丑,那么我们就来美化下吧,加一些图片

#import <UIKit/UIKit.h>
 
/*tableview 一定要用到这两个delegate  UITableViewDataSource,UITableViewDelegate */
@interface ViewController :UIViewController <UITableViewDataSource,UITableViewDelegate>
{
    UITableView *tableview;
    NSArray *array;
    NSArray *arrayImage;
   NSArray *arrayImage1;
    
}
@property (strong,nonatomic)UITableView *tableview;
@property (strong,nonatomic)NSArray *array;
@property (strong,nonatomic)NSArray *arrayImage;
@property (strong,nonatomic)NSArray *arrayImage1;
@end
#import "ViewController.h"

@interfaceViewController ()

@end

@implementation ViewController
@synthesize tableview;
@synthesize array;
@synthesize arrayImage;
@synthesize arrayImage1;
- (void)viewDidLoad
{
    [superviewDidLoad];
tableview = [[UITableViewalloc]initWithFrame:CGRectMake(0, 0,self.view.bounds.size.width,self.view.bounds.size.height)style:UITableViewStyleGrouped];
    
//    UITableViewStylePlain,                
//    UITableViewStyleGrouped
  
    tableview.delegate =self;//不要忘写了这两句话哟调用delegate*/
    tableview.dataSource=self;

  /*改变 UITableViewStyleGrouped 样式的 背景色 */
   tableview.backgroundView = [[UIViewalloc]init];
   tableview.backgroundColor = [UIColorclearColor];
   
    [self.viewaddSubview:tableview];
    
    NSMutableArray *arrayValue = [[NSMutableArrayalloc]init];
    NSMutableArray *arrayImageValue = [[NSMutableArrayalloc]init];
    NSMutableArray *arrayImageValue2 = [[NSMutableArrayalloc]init];
    for (int i = 1; i<= 5; i++)
    {
        NSString *value = [NSStringstringWithFormat:@"%d",i];
        NSString *imageName = [NSStringstringWithFormat:@"image%@.png",value];
        UIImage *image = [UIImageimageNamed:imageName];
        NSLog(@"imageName == %@",imageName);
        [arrayValue addObject:value];
        [arrayImageValue
addObject:image];
    }
    

    for (int i = 6;i<=10; i++ )
    {
        NSString *value = [NSStringstringWithFormat:@"%d",i];
        NSString *imageName = [NSStringstringWithFormat:@"image%@.png",value];
        UIImage *image = [UIImageimageNamed:imageName];
        [arrayImageValue2
addObject:image];

    }
   array = arrayValue;
   arrayImage = arrayImageValue;
   arrayImage1 =arrayImageValue2;
}

/* 这个函数是显示tableview的章节数*/
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
    return 2;
}
/* 这个函数是指定显示多少cells*/
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [arraycount];//这个是指定加载数据的多少即显示多少个cell,如过这个地方弄错了会崩溃的哟
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //定义个静态字符串为了防止与其他类的tableivew重复
    static
NSString *CellIdentifier =@"Cell";  
    //定义cell的复用性当处理大量数据时减少内存开销
    UITableViewCell *cell = [tableviewdequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell ==nil)
    {
               cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier];
    }
    
    if (indexPath.section == 0)
    {
                cell.imageView.image = [arrayImageobjectAtIndex:[indexPathrow]]; 
    }
    else
    {
              cell.imageView.image = [arrayImage1objectAtIndex:[indexPathrow]];
    }
    cell.backgroundColor = [UIColorgrayColor];
    
    cell.detailTextLabel.text = [arrayobjectAtIndex:[indexPathrow]];
    
    cell.textLabel.text  =  [arrayobjectAtIndex:[indexPathrow]];
    
    return cell;
}

- (void)didReceiveMemoryWarning
{
    [superdidReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
是不是比以前好多了



有些时候我们不想要那个方框或者定义的时候想换成自己的背景图片

那么我们就在下面的函数里加两句代码吧

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    //定义个静态字符串为了防止与其他类的tableivew重复
    static
NSString *CellIdentifier = @"Cell";  
    //定义cell的复用性当处理大量数据时减少内存开销
    UITableViewCell *cell = [tableviewdequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell ==nil)
    {
        
        cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier];
    }
    
    if (indexPath.section == 0)
    {
       
        cell.imageView.image = [arrayImageobjectAtIndex:[indexPathrow]];
     
    }
    else
    {
        cell.imageView.image = [arrayImage1objectAtIndex:[indexPathrow]];

    }
    
    /*去掉方框 ,或者把view 换成imageview */
    UIView *backView = [[UIViewalloc]init];
   [cell
setBackgroundView:backView];
    cell.backgroundColor = [UIColorclearColor];

    
    cell.detailTextLabel.text = [arrayobjectAtIndex:[indexPathrow]];
    
    cell.textLabel.text  =  [arrayobjectAtIndex:[indexPathrow]];
    
    return cell;
}
看下效果吧



现在我们把代码改成  UITableViewStylePlain 样式的在加些处理

我们给tableview换个背景图片吧!我们在

- (void)viewDidLoad 函数里加上这几行代码吧
  {
    UIImage *backImageName= [UIImageimageNamed:@"iOS 7 wall 3.png"];
    UIImageView *taleviewBackImageView = [[UIImageViewalloc]initWithImage:backImageName];
    [self.tableviewsetBackgroundView:taleviewBackImageView];

}

这回我们看下效果是不是更好了!



当我们选中tableview 的时候会发现是蓝色背景的!这个是有几个属性的我在这里就先不介绍了!在玩一些应用的时候我们会发现点击列表的时候会改变颜色,表示你选中的那个cell ,其实是都是一些图片构成的!
现在我添加一个跟cell一样宽的图片!

我们只需要在下面的函数里加上两行代码就ok了效果如下图

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    UIImageView *cellimageView = [[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"nav_on.png"]];
    cell.selectedBackgroundView = cellimageView;
    
}



下面是系统默认的点击颜色
//无色  
cell.selectionStyle = UITableViewCellSelectionStyleNone;  
//蓝色 ,系统默认是蓝色的 
cell.selectionStyle = UITableViewCellSelectionStyleBlue;  
//灰色  
cell.selectionStyle = UITableViewCellSelectionStyleGray;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: