如何将scrollview添加在tableview的cell中,具体看原文

如何将scrollview添加在tableview的cell中,具体看原文,第1张

我做了一个scollview的图片横向滚动浏览demo(scrollview是用ib拖的) 想要把它放在新建的tableview的每个cell中

UIScrollView * view = [[UIScrollView alloc] initWithFrame:CGRectMake(5, 0, 295, 40)]

view.backgroundColor = [UIColor redColor]

[view setContentSize:CGSizeMake(600, 40)]

[cell.contentView addSubview:view]

[view release]

这个只是加一个简单的scrollview

找到填充cell的委托,然后把UIScrollView 添加到cell里面去。[cell addsubview:view ]

1.新建TableViewCell类,继承父类为UITableViewCell

1.1 "TableCell.h"

#import <UIKit/UIKit.h>

/**

* 房间桌子显示结构

* /

@interface TableCell : UITableViewCell {

  UILabel *tableNoLable// 桌子号

  UIImageView *tableImageView// 桌子图片

UIImageView *tableStateImageView// 桌子状态图片

@property (nonatomic ,retain) IBOutlet UILabel *tableNoLable// 桌子号

@property (nonatomic ,retain) IBOutlet UIImageView *tableImageView// 桌子图片

@property (nonatomic ,retain) IBOutlet UIImageView *tableStateImageView// 桌子状态图片

1.2 TableCell. m

#import "TableCell.h"

@implementation TableCell

@synthesize tableNoLable// 桌子号

@synthesize tableImageView// 桌子图片

@synthesize tableStateImageView// 桌子状态图片

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {

// Initialization code

}

return self

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

[super setSelected:selected animated:animated]

// Configure the view for the selected state

}

- (void)dealloc {

[tableNoLable release]

[tableImageView release]

[hand3ImageView release]

[super dealloc]

}

@end

1.3 布置布局文件( xib 文件)指明 class 为自己定义的 tabelcellview

new 一个Empty Xib文件,其中不包含view, 在libriary中拖一个UITableCell到两个图标的框中,双击cellview,指明class为cell.m文件,开始编辑

2. 页面中在 tableView 中加载自己的 cell

#import "TableCell.h"

// 设置房间桌子数

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

[Util showLog:@"numberOfRowsInSection"]

return g_Room.wTableCount

}

// 设置单元格的高度

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

[Util showLog:@"heightForRowAtIndexPath"]

return kTableViewRowHeight

}

// 设置单元个样式

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

[Util showLog:@"cellForRowAtIndexPath start"]

static NSString *tableCellIdentifier = @"TableCellIdentifier"

TableCell *cell = (TableCell *)[tableView dequeueReusableCellWithIdentifier:tableCellIdentifier]

[Util showLog:@"TableCellIdentifier"]

if(cell == nil){

NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"TableCell" owner:self options:nil]

for(id oneObject in nib){

if([oneObject isKindOfClass:[TableCell class]]){

cell = (TableCell *)oneObject

//***** 自己设计每个 cell 的内容,此函数会回调 table 的行数次,行数为 numberOfRowsInSection:(NSInteger)section 函数指定

// 显示桌子号码

NSUInteger row = [indexPath row] + 1

NSString *str =[NSString stringWithFormat:@"%d",row]

cell.tableNoLable.text = [[@" 第 " stringByAppendingString:str] stringByAppendingString:@" 桌 "]

[Util showLog:@"tableNoLable"]

if(deskshowFlg){

// 获取要绘画的桌子信息

Desk *tempDesk = [g_DeskArray objectAtIndex:[indexPath row]]

}

}

}

XIB里没试过,不过用代码肯定是可以的,用这个方法:- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/7954533.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-11
下一篇2023-04-11

发表评论

登录后才能评论

评论列表(0条)

    保存