最近項目中需要實現如下圖所示的效果:
通過界面我們斷定是一個UITableView,分成三部分,第一部分是全天,第二部分是上午,第三部分是下午。最主要的是AM和PM中也是列表,這個就比較複雜了。我的做法是在Iphone在table cell中添加自定義佈局view這篇文章的基礎上製作更復雜的界面。具體的過程如下:
- 創建UITableViewCell的自定義類,這個就不用說了,在之前的博客中介紹過。
- 在創建的cell中添加一個新的UITableView。
在自定義的cell中添加組建,我的類是MyProfileTableViewCell,在這個中添加:
IBOutlet UITableView *myTaleView;
IBOutlet UILabel *lable;
實現相應的set get方法。在和IB中相應的組建相連。
- 在tableview中引入相應的cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CustomCellIdentifier";
if ([indexPath section]==0) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellSelectionStyleGray
reuseIdentifier:CellIdentifier] autorelease];
}
return cell;
}else {
MyProfileTableViewCell *cell = (MyProfileTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"MyProfileTableViewCell" owner:self options:nil];
cell = [array objectAtIndex:0];
[cell setSelectionStyle:UITableViewCellSelectionStyleGray];
if ([indexPath section]==1) {
[[cell lable] setText:@"AM"];
}
if ([indexPath section]==2) {
[[cell lable] setText:@"PM"];
}
return cell;
}
}
-
在相應的cell中添加UITableView相應的Delegate和DataSource,我的cell完整的聲明如下:
#import <UIKit/UIKit.h>
@interface MyProfileTableViewCell : UITableViewCell
<UITableViewDelegate,UITableViewDataSource>{
IBOutlet UITableView *myTaleView;
IBOutlet UILabel *lable;
}
@property (nonatomic,retain) UITableView *myTaleView;
@property (nonatomic,retain) UILabel *lable;
@end -
在添加相應的協議函數即可:
#import "MyProfileTableViewCell.h"
#import "MyTableViewCell.h"
@implementation MyProfileTableViewCell
@synthesize myTaleView,lable;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
}
- (void)dealloc {
[self.lable release];
[self.myTaleView release];
[super dealloc];
}
- (NSInteger)tableView:(UITableView *)tableView1 numberOfRowsInSection:(NSInteger)section {
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CustomCellIdentifier";
MyTableViewCell *cell = (MyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"MyTableViewCell" owner:self options:nil];
cell = [array objectAtIndex:0];
[cell setSelectionStyle:UITableViewCellSelectionStyleGray];
}
[[cell label] setText:@"10:00"];
[[cell _content] setText:@"早上起來賣大米,賣了一筐大大米。\n早上起來賣大米,賣了一筐大大米。"];
return cell;
}
- (CGFloat)tableView:(UITableView *)atableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 56;
}
@end做項目的時候會用的這種情況,把找到的資料再次分享給大家,傳播技術人人有責。