2012-02-07 62 views
1

我想添加一个不同的图标图像,即image1.png,image2.png等到以下UITableview阵列。真的需要某人的帮助。提前致谢。将图标图像添加到UITableview阵列

{ 
self = [super init]; 
if (self) { 
    // Custom initialization 
    self.tableData = [NSArray arrayWithObjects:@"Region", @"Subregion", @"Country",  @"County", @"City", @"District", nil]; 

    CGRect frame = self.view.bounds; 
    frame.size.height -= 100; 
    self.tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStyleGrouped]; 
    [self.tableView setBackgroundColor:[UIColor clearColor]]; 
    [self.tableView setDataSource:self]; 
    [self.tableView setDelegate:self]; 
    [self.tableView setScrollEnabled:NO]; 

    [self.view addSubview:self.tableView]; 
} 
return self; 
} 
+1

您创建的cellForRowAtIndexPath单独的UITableViewCell实例:这是UITableViewDataSource协议的一部分。 – Rayfleck 2012-02-07 13:15:10

回答

4

使用的cellForRowAtIndexPath:

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 

    // Configure the cell. 
    cell.textLabel.text = @"cell text"; 
    cell.imageView.image = [UIImage imageNamed:@"image1.png"]; 
    return cell; 
} 
4

你可以创建另一个数组保存图片

self.tablePicture = [NSArray arrayWithObjects:@"pic1.png", @"pic2.png", @"Country.png", nil]; 

你要显示的顺序,并在的cellForRowAtIndexPath的名字只是写

cell.imageView.image = [UIImage imageNamed:[tablePicture objectAtIndex:indexPath.row]]; 
+0

非常感谢,我如何将上面的数组应用到我的代码中,剩下的就剩下了。 – CraigBellamy 2012-02-07 13:29:58

+0

你只需要在头文件中声明数组并合成它就像tableData数组 – Radu 2012-02-07 14:56:41

+0

我将如何在头文件中声明数组?对不起,听起来很愚蠢,但做得不好! – CraigBellamy 2012-02-07 17:40:29

1

您可以利用创建自定义类的oop样式(例如DataItem)并使用DataItem元素初始化您显示的数组。换句话说,您可以创建一个包含名称和图像元素的模型。

例如:

//.h 
@interface DataItem : NSObject 
{ 
    NSString* name; 
    NSString* thunbmail; 
} 

@property (nonatomic, copy) NSString* name; 
@property (nonatomic, copy) NSString* thunbmail; 

- (id)initWithName:(NSString*)dName withThunbmail:(NSString*)dThunbmail; 

@end 

//.m 
@implementation DataItem 

@synthesize name; 
@synthesize thunbmail; 

- (id)initWithName:(NSString*)dName withThunbmail:(NSString*)dThunbmail 
{ 
    if(self = [super init]) 
    { 
     name = [dName copy]; // release in dealloc!! 
     thunbmail = [dThunbmail copy]; // release in dealloc!! 
    } 
    return self; 
} 

// create dealloc here 

@end 

现在你可以初始化一个物品,并把它添加到阵列(也可能是最好有一个NSMutableArray)像下面这样:

DataItem* di = [[DataItem alloc] initWithName:@"name" withThunbmail:@"image.png"]; 

NSMutableArray* arrData = [[NSMutableArray alloc] init]; 
[arrData addObject:di]; 

// add other object here 

self.tableData = arrData; 

// release memory... 

,然后在cellForRowAtIndexPath

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 

    // Configure the cell.. 

    DataItem* di = (DataItem*)[self.tableData objectAtIndex:[indexPath row]]; 
    cell.textLabel.text = di.name; 
    cell.imageView.image = [UIImage imageNamed:di.thunbmail]; 

    return cell; 
} 

这是一种优雅的方式来封闭你的cont在一个单一的班级模型。

希望它有帮助。

P.S.检查代码。我手写的。

+1

这应该是接受的答案...! – Shailesh 2013-04-05 07:11:18

+0

@shailesh谢谢 – 2013-04-07 20:13:13