频道栏目
首页 > 资讯 > IOS > 正文

UITableViewController表视图建立过程中自定义单元格的创建,通过URL地址下载图片

13-12-26        来源:[db:作者]  
收藏   我要投稿

刚刚学过表视图建立的朋友,一定都很想创建一些自己定义的单元格,以满足自己的个性化需求,其实这个不难,下面我来和大家分享下,另外呢,我新添加了一个功能,就是,每个单元格的内容,里面的填充的图片是我通过URL地址下载得到的。

在代码实现之前我们需要建立一个plist文件来保存我们需要的图片的URL地址。

具体步骤如下




打开root.plist文件,如下所示:


在里面建立四个item,Root的类型为Array

item类型为NSString , 然后在后面粘贴上复制来的图片的URL地址。

效果如下所示:


具体代码如下:

HHLAppDelegate.h

#import 

//@class HHLViewController;

@interface HHLAppDelegate : UIResponder 

@property (strong, nonatomic) UIWindow *window;

@end


HHLAppDelegate.m


#import "HHLAppDelegate.h"

//#import "HHLViewController.h"

#import "tableViewController.h"

@implementation HHLAppDelegate

- (void)dealloc
{
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    
   //HHLViewController *viewController = [[HHLViewController alloc]initWithNibName:@"HHLViewController" bundle:nil];
    //HHLViewController2 *viewController2 = [[HHLViewController2 alloc]initWithNibName:nil bundle:nil];
    
    tableViewController *tableView = [[tableViewController alloc]initWithNibName:nil bundle:nil];
    
    UITabBarController *pTabbarVC = [[UITabBarController alloc]init];
    pTabbarVC.viewControllers = [NSArray arrayWithObjects:viewController,viewController2,tableView, nil];
    self.window.rootViewController = pTabbarVC;
    [tableView release];
    [pTabbarVC release];
    //[viewController2 release];
    //[viewController release];
    
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end



tableViewController.h


#import 

@interface tableViewController : UIViewController

@property (retain,nonatomic) UITableView *pTableView;

@property (retain,nonatomic) NSArray *pArr;

@property (retain,nonatomic) NSOperationQueue *pQueue;

@end



tableViewController.m


#import "tableViewController.h"
#import "HHLCell.h"

@interface tableViewController ()

@end

@implementation tableViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tabBarItem.title = @"ThreadThreee";
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.pTableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.pTableView.dataSource = self;
    self.pTableView.delegate = self;
    [self.view addSubview:self.pTableView];
    
    NSString *pPath = [[NSBundle  mainBundle] pathForResource:@"root" ofType:@"plist"];
    self.pArr = [[NSArray alloc] initWithContentsOfFile:pPath];
     self.pQueue =[[NSOperationQueue alloc]init];
    
}

//
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.pArr count];
}
//
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"identifier";
    HHLCell *pCell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (nil ==pCell) {
        //pCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        pCell =[[HHLCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        
    }
    
    //NSInteger row = [indexPath row];
   // pCell.textLabel.text = [self.pArr objectAtIndex:row];
    //pCell.mAc
    
        
    
    
    [pCell.mActivityIV startAnimating];
    NSInvocationOperation *Invocation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(downLoadImage:) object:indexPath];
    [self.pQueue addOperation:Invocation];
    
    return pCell;
    
}
- (void)downLoadImage:(NSIndexPath *)path
{
    NSInteger row = [path row];
    NSString *pStr = [self.pArr objectAtIndex:row];
    NSURL *pURL = [NSURL URLWithString:pStr];
    NSData *pData = [NSData dataWithContentsOfURL:pURL];
   
    UIImage *pImage = [UIImage imageWithData:pData];
     HHLCell *pCell = (HHLCell *)[self.pTableView cellForRowAtIndexPath:path];
    
    pCell.imageView.image = pImage;
    [pCell.mActivityIV stopAnimating];
    [pCell.mActivityIV removeFromSuperview];
    
    
}
//每行的高度
- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}
- (void)dealloc{
    [_pTableView release];
    [super dealloc];
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


HHLCell.h


#import 

@interface HHLCell : UITableViewCell

@property (retain,nonatomic) UIActivityIndicatorView *mActivityIV;

@end


HHLCell.m


#import "HHLCell.h"

@implementation HHLCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.mActivityIV = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        [self.mActivityIV setFrame:CGRectMake(10, 10, 35, 35)];
        [self addSubview: self.mActivityIV];
       
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end





相关TAG标签
上一篇:非本地跳转函数setjmp,longjmp, sigsetjmp, siglongjmp
下一篇:windows phone应用程序版本升级
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站