ios uitableviewcrotroller 怎么放按钮

ios uitableviewcrotroller 怎么放按钮,第1张

1.首先创建一个Single View Project,命名为UITableViewControllerTest。打开ViewController.xib并拖入一个UITableView,选中该视图,并点击Connections inspector,分别将delegate和dataSource连线到File's owner

2.在AppDelegate.h中添加属性

@property (strong, nonatomic) IBOutlet UITabBarController * rootViewController

并在.m文件中添加合成器

@synthesize rootViewController

然后在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中添加该TableView为subView

3.在ViewController.h文件中添加协议UITableViewDelegate,UITableViewDataSource,然后在.m文件中实现协议中的方法

#pragma mark -

#pragma mark Table View Data Source Methods

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

{

return [self.listDatas count]

}

上面的方法是返回Table中的行数

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

{

static NSString * SimpleTableViewIdentifier =@"SimpleTableViewIdentifier"

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableViewIdentifier]

if (cell == nil) {

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableViewIdentifier]

}

UIImage * img = [UIImage imageNamed:@"singleicon"]

cell.imageView.image = img

NSUInteger row = [indexPath row]

cell.textLabel.text = [listDatas objectAtIndex:row]

return cell

}

上面的方法是构造每一个cell,也就是每一行,SimpleTableViewIdentifier是标记,dequeueReusableCellWithIdentifier方法是当有可重用的cell时,根据标记直接取,而不用重新生成,可以提高效率。

NSIndexPath封装了有关行和分区的信息

UIImage * img = [UIImage imageNamed:@"singleicon"]

cell.imageView.image = img

NSUInteger row = [indexPath row]

cell.textLabel.text = [listDatas objectAtIndex:row]

这段代码分别设置了行的图片和文字,另外,通过accessoryType属性可以设置行的附属属性,有几种样式可以提供选择。也可以自定义附属视图:cell.accessoryView = [[myView alloc] init]

4.在viewDidLoad方法中添加NSArray后,填入数据,然后run,可以看打运行结果

在这里我使用了TabBarController来承载三种类型的tableview,分别是Plain、Group以及Custom自定义的

5.下面看第二种样式,分组的形式

使用分组的话还要添加一个方法,返回一共有多少组

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

另外可以设置分组标题

//返回组的名称

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{

if ([keys count] == 0) {

returnnil

}

NSString * key = [keys objectAtIndex:section]

return key

}

//设置行的高度

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

{

return60

}

#pragma mark Table Delegate Methods

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

NSUInteger row = [indexPath row]

NSString * message = [[NSString alloc]initWithFormat:@"You selected is %@!",[listDatas objectAtIndex:row]]

UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"Information" message:message delegate:nilcancelButtonTitle:@"Confirm" otherButtonTitles:nil,nil]

[alert show]

}

根据tag值进行获取,在这里设置tag值, 然后在方法 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 中进行获取这个tag值,获取方法是: UIButton *exitBtn = (UIButton *)[cell viewWithTag:1]//1是我上面设置的tag值 这样就获取到了,你再给他添加action事件就可以了,


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

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

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

发表评论

登录后才能评论

评论列表(0条)

    保存