0

我想为单元格显示图像。但是这段代码显示,只有白色的tableview。为什么?请告诉我。(我不使用故事板。)

表格单元格.h

#import <UIKit/UIKit.h>

@interface TableCell : UITableViewCell <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,retain) UITableView *tableCellView;
@property (nonatomic,retain) NSArray *cellArray;

-(NSString *)reuseIdentifier;

@end

表格单元格.m

    #import "TableCell.h"

@implementation TableCell

@synthesize tableCellView;
@synthesize cellArray;


-(NSString *)reuseIdentifier
{
    return @"cell";
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [self.tableCellView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    for (UIImageView *view in cell.subviews)
    {
        [view removeFromSuperview];
    }

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
    imageView.image = [UIImage imageNamed:[cellArray objectAtIndex:indexPath.row]];
    imageView.contentMode = UIViewContentModeCenter; // 画像サイズを合わせて貼る

    CGAffineTransform rotateImage = CGAffineTransformMakeRotation(M_PI_2); 
    imageView.transform = rotateImage; 

    [cell addSubview:imageView];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return 220;
}

@end

表视图控制器.h

    #import <UIKit/UIKit.h>

@class TableCell;

@interface TableViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) TableCell *tableViewCell;
@property (nonatomic, retain) NSArray *titlesArray;
@property (nonatomic, retain) NSArray *peopleArray;
@property (nonatomic, retain) NSArray *thingsArray;
@property (nonatomic, retain) NSArray *fruitsArray;
@property (nonatomic, retain) NSArray *arrays;

@end

表视图控制器.m

#import "TableViewController.h"
#import "TableCell.h"

@interface TableViewController ()

@end

@implementation TableViewController

@synthesize tableView;
@synthesize tableViewCell;
@synthesize titlesArray;
@synthesize peopleArray;
@synthesize thingsArray;
@synthesize fruitsArray;
@synthesize arrays;

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.tableView.delegate = self;
    self.tableView.dataSource = self;

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

    titlesArray = [[NSArray alloc] initWithObjects:@"People", @"Things", @"Fruits", nil];
    peopleArray = [[NSArray alloc] initWithObjects:@"Gardener.png", @"Plumber.png", @"BusinessWoman.png", @"BusinessMan.png", @"Chef.png", @"Doctor.png", nil];
    thingsArray = [[NSArray alloc] initWithObjects:@"StopWatch.png", @"TrashCan.png", @"Key.png", @"Telephone.png", @"ChalkBoard.png", @"Bucket.png", nil];
    fruitsArray = [[NSArray alloc] initWithObjects:@"Pineapple.png", @"Orange.png", @"Apple.png", nil];

    arrays = [[NSArray alloc] initWithObjects:peopleArray, thingsArray, fruitsArray, nil];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return [arrays count];
}

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

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [titlesArray objectAtIndex:section];
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    TableCell *cell = (TableCell *)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:self options:nil];

        CGAffineTransform rotateTable = CGAffineTransformMakeRotation(-M_PI_2);
        tableViewCell.tableCellView.transform = rotateTable;
        tableViewCell.tableCellView.frame = CGRectMake(0, 0, tableViewCell.tableCellView.frame.size.width, tableViewCell.tableCellView.frame.size.height);

        tableViewCell.cellArray = [arrays objectAtIndex:indexPath.section];

        tableViewCell.tableCellView.allowsSelection = YES;
        cell = tableViewCell;

    }

    return cell;
}

@end

AppDelegate.m

TableViewController *tvc = [[TableViewController alloc] init];
    self.window.rootViewController = tvc;
4

3 回答 3

0

如果 UITableViewCell 的样式是默认的,则每个 UITableViewCell 都有图像支持。

cell.imageView.image = yourImageView.image;

你不需要做任何其他事情。

如果需要,您可以通过更改 yourImageView 设置来更改图像设置。

于 2012-12-13T09:48:48.177 回答
0

您的numberOfSectionsInTableView方法可能返回 0。检查[arrays count]. 还有一件事是你没有UITableViewCell以正确的方式被创造出来。这也可能是问题所在。检查此链接以创建自定义UITableViewCell

于 2012-12-13T09:35:22.767 回答
0

UITableViewCell 默认已经支持图片。您不需要添加新的 UIImageView 作为子视图...

所以代替这个:

for (UIImageView *view in cell.subviews)
{
    [view removeFromSuperview];
}

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
imageView.image = [UIImage imageNamed:[cellArray objectAtIndex:indexPath.row]];
imageView.contentMode = UIViewContentModeCenter; // 画像サイズを合わせて貼る

CGAffineTransform rotateImage = CGAffineTransformMakeRotation(M_PI_2); 
imageView.transform = rotateImage; 

[cell addSubview:imageView];

写这个:

cell.imageView.image = [UIImage imageNamed:[cellArray objectAtIndex:indexPath.row]];

哦,我现在注意到你的代码有很多其他的东西有问题!很抱歉这么说。您需要更改的其他内容:

  1. 将此方法从您的 TableCell 复制到您的 TableViewController:- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath并删除现有的
  2. 删除 TableCell 类

我看到你也在做轮换......但现在把它放在一边。首先尝试使其工作,然后担心图像的旋转。

我可以建议你读一本关于 iOS 开发的书吗?好像你根本不知道自己在做什么:(

但我很想帮你。你能把你所有的代码放在http://www.github.com上并邀请我作为合作者吗?我的用户名是 tomvanzummeren。我可以让这个应用程序为你工作。

于 2012-12-13T09:37:40.303 回答