2012-12-14 33 views
-2

enter image description here ViewController.h的UITableView如何在tableview中显示图像

#import <UIKit/UIKit.h> 

@interface TableViewController : UIViewController <UITableViewDataSource,UITableViewDelegate> 
{ 
    UITableView *table; 
    NSArray *tableData; 
} 
@end 

ViewController.m

#import "TableViewController.h" 

@interface TableViewController() 

@end 

@implementation TableViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    table.delegate = self; 
    table.dataSource = self; 

    table = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewCellStyleDefault]; 
    [self.view addSubview:table]; 


    tableData = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"StopWatch.png"],[UIImage imageNamed:@"TrashCan.png"],[UIImage imageNamed:@"Key.png"],[UIImage imageNamed:@"Telephone.png"],[UIImage imageNamed:@"ChalkBoard.png"],[UIImage imageNamed:@"Bucket.png"],nil]; 

} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [tableData count]; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *simpleTableIdentifier = @"SimpleTableItem"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 

    if (cell == nil) 
    { 
     //cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]; 
    } 

    cell.imageView.image = [tableData objectAtIndex:indexPath.row]; 
    return cell; 
} 

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    NSString *str = @"TableViewSection"; 
    return str; 
} 

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
{ 
    return 22; 
} 

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 80; 
} 

@end 

我想有显示,在实现代码如下细胞此图像。

请告诉我,如何在桌面视图单元上显示图像。

我的风格只是编码。我不使用故事板。

+0

是不是显示? – vishy

+0

代码没问题,这是什么问题? –

+0

图片转换成NSArray。它是tableview上的未显示图像。我想知道为什么? :( – onion

回答

1

你的对象初始化之前分配delegatedataSource性能,请尝试更改顺序,就像这样:

table = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewCellStyleDefault]; 
table.delegate = self; 
table.dataSource = self; 
+0

我尝试了你的建议,但我搞砸了。出去吃饭... – onion

+0

抱歉,但我不明白你的意思 – tkanzakic

0

你有什么数据源? xml tableview cell

这样静态 cell.imageView.image = [UIImage imageNamed:@“Maps-icon.png”]; cell.imageView.highlightedImage = [UIImage imageNamed:@“Maps-icon.png”]; 或

[cell.imageView setImageWithURL:[NSURL URLWithString:[newsItem objectForKey:@"image"]] 
       placeholderImage:[UIImage imageNamed:@"placeholder"]]; 

newsItem的NSDictionary

的NSDictionary * newsItem = [soapSource objectAtIndex:indexPath.row];

相关问题