用UITableViewCell实现电子书平铺书架结果

用UITableViewCell实现电子书平铺书架结果
2011-09-16


原创文章,如需转载请注明:转载自:梢公法度 http://www.helmsmansoft.com/index.php/archives/70 

现阶段网上讲解的实现这种结果的文章一般都是用UIVIew 去加载UIButton实现此功能,今天给大师说一下在UITableViewCell下如何去实现这种结果 

UITableViewCell默认是列表形态,默认状况下容许加载一张图片,若是想实如今UITableViewCell中显示多行图片,可以自定义UIView,把UIView加载到Cell上即可实现这种结果。这里首要说一下怎么去写Cell里的功能,其它功能不具体说了然。 

具体见代码: 
在theTableViewController.h中定义UIButton *buttonArray; 
在theTableViewController.m文件内定义Cell中显示的图书本数 

#define theNumberOfTatleTile 3 //每行显示3本 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section //每行3本,返回的行数 

if([self.listFileName count]%theNumberOfTatleTile == 0){ //listFileName 图书名字数组 
return [self.listFileName count]/theNumberOfTatleTile; 
}else{ 
return [self.listFileName count]/theNumberOfTatleTile+1; 



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

static NSString *theKey = @"theKey"; 
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; //重视此处 


cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:theKey] autorelease]; 

UILabel *customLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 768, 1004)]; 
customLabel.backgroundColor = [UIColor redColor]; 
buttonArray = [[NSMutableArray alloc] init]; 
NSUInteger row = [indexPath row]; 
for(int i = 0 ; i < [self.listFileName count]/theNumberOfTatleTile;i++){ 
NSNumber *cellRow = [[NSNumber alloc] initWithInt:theNumberOfTatleTile]; 
[buttonArray addObject:cellRow]; 

for(int j = 0 ; j < [self.listFileName count]%theNumberOfTatleTile; j++){ 
NSString *str = [NSString stringWithFormat:@"%d",[self.listFileName count]%theNumberOfTatleTile]; 
[buttonArray addObject:str]; 


cell.imageView.image = NULL; 
cell.textLabel.text = NULL; 
cell.detailTextLabel.text = NULL; 
NSString *a = [buttonArray objectAtIndex:row]; 
NSInteger b= [a integerValue]; 

for(int m = 0 ; m < b; m++){ 

button = [[UIButton alloc] initWithFrame:CGRectMake(70+m%theNumberOfTatleTile*240, 20, 150, 200)]; 
UILabel *label= [[UILabel alloc] initWithFrame:CGRectMake(0, 170, 150, 30)]; 
[button setBackgroundImage:[UIImage imageNamed:@"apple.png"] forState:UIControlStateNormal]; 
label.backgroundColor = [UIColor clearColor]; 
label.text = [listFileName objectAtIndex:theNumberOfTatleTile*row+m]; 
[button addSubview:label]; 

button.backgroundColor = [UIColor whiteColor]; 
button.reversesTitleShadowWhenHighlighted = YES; 
[button setTag:theNumberOfTatleTile*row+m]; 
[button addTarget:self action:@or(thePicButtonPress:) forControlEvents:UIControlEventTouchUpInside]; 
// button.userInteractionEnabled = YES; 
// [button setTitleShadowColor:[UIColor clearColor] forState:UIControlEventTouchUpInside]; 
// [button setTitleShadowColor:[UIColor blackColor] forState:UIControlStateNormal]; 
// 
[cell addSubview:button]; 
[button release]; 


self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; 
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"2.png"]]; 
cell.backgroundView = imgView; 
cell.ionStyle = UITableViewCellEditingStyleNone; //选中CELL后结果 
return cell; 




你可能感兴趣的:(用UITableViewCell实现电子书平铺书架结果)