ios--自定义UITableView带多个按钮(示例)

一、自定义UITableViewCell

1、新建一个xxxUIViewController类,带xib文件的。

2、删除xib文件中的View,拖一个UITableViewCell控件,在里面放需要的控件。并将xib文件绑定到xxxUIViewContrller。

绑定如图

其中.h文件的代码为:(将UIViewController改为UITableViewCell)

#import <UIKit/UIKit.h>

@interface AloneSiteListCell : UITableViewCell
@property(nonatomic) IBOutlet UILabel *lbltitle;
@property(nonatomic) IBOutlet UILabel *lblpersons;
@property(nonatomic) IBOutlet UILabel *lblcircles;
@property(nonatomic) IBOutlet UILabel *lblprizes;
@property(nonatomic) IBOutlet UILabel *lblone;
@property(nonatomic) IBOutlet UILabel *lbltwo;
@property(nonatomic) IBOutlet UILabel *lblthree;
@property(nonatomic) IBOutlet UILabel *lblfour;
@property(nonatomic) IBOutlet UILabel *lblfive;
@property(nonatomic) IBOutlet UILabel *lblsix;
@property(nonatomic) IBOutlet UIButton *btnEdit;
@property(nonatomic) IBOutlet UIButton *btnDelete;
@end


.m文件的代码为:

#import "AloneSiteListCell.h"

@implementation AloneSiteListCell
@synthesize lblcircles,lblsix,lblfive,lblfour,lblthree,lbltwo,lblone,lblprizes,lblpersons,lbltitle;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end


3、将xib视图控件与xxxViewController连接:

二、绑定数据并显示UITableView

.h文件:

#import <UIKit/UIKit.h>
#import "SqlDatabaseManager.h"
#import "MBProgressHUD.h"

@interface AloneSiteListViewController : UIViewController<MBProgressHUDDelegate,UITableViewDelegate>
{
    MBProgressHUD *HUD;
    NSMutableArray *tableviewData;
    NSMutableArray *tableviewSections;
    SqlDatabaseManager *sqlDBM;
    NSIndexPath *selectindex;
}
@property(nonatomic,strong) IBOutlet UITableView *tableview;

-(IBAction)backOff:(id)sender;
-(IBAction)newSite:(id)sender;
-(IBAction)cellclickEdit:(id)sender;
-(IBAction)cellclickDelete:(id)sender;
@end

.m文件:

#import "AloneSiteListViewController.h"
#import "AloneSetSiteViewController.h"
#import "AloneSiteListCell.h"
#import "SqlDatabaseManager.h"

@interface AloneSiteListViewController ()

@end

@implementation AloneSiteListViewController
@synthesize tableview;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    UILabel *titleText = [[UILabel alloc] initWithFrame: CGRectMake(100, 0, 180, 50)];
    titleText.backgroundColor = [UIColor clearColor];
    titleText.textColor=[UIColor whiteColor];
    titleText.textAlignment=UITextAlignmentCenter;
    [titleText setFont:[UIFont systemFontOfSize:17.0]];
    [titleText setText:@"单机场地管理"];
    self.navigationItem.titleView=titleText;
    
    //自定义返回
    UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(0.0, 100.0, 49.0, 29.0)];
    [backBtn setBackgroundImage:[UIImage imageNamed:@"上导航-左返回.png"] forState:UIControlStateNormal];
    [backBtn.titleLabel setFont:[UIFont fontWithName:@"Helvetica" size:12]];
    [backBtn addTarget:self action:@selector(backOff:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backButtonItem=[[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = backButtonItem;
    
    //自定义新增按钮
    UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(0.0, 100.0, 49.0, 29.0)];
    [rightBtn setBackgroundImage:[UIImage imageNamed:@"上导航-按钮.png"] forState:UIControlStateNormal];
    [rightBtn setTitle:@"新增" forState:UIControlStateNormal];
    [rightBtn.titleLabel setFont:[UIFont boldSystemFontOfSize:13]];
    [rightBtn addTarget:self action:@selector(newSite:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightButtonItem=[[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rightButtonItem;
    [self.tableview setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.tableview setSeparatorColor:[UIColor clearColor]];
    [self.tableview setBackgroundView:nil];
    self.tableview.backgroundColor =  [UIColor clearColor];
    
    tableviewSections=[[NSMutableArray alloc] init];
    tableviewData=[[NSMutableArray alloc] init];
    self.tableview.backgroundColor = nil;
    
    sqlDBM=[[SqlDatabaseManager alloc] init];
    
    if ([self respondsToSelector:@selector(myTask)]){
        [NSThread detachNewThreadSelector:@selector(myTask) toTarget:self withObject:nil];
    }
}
// 获取网络数据
- (void)myTask {
    HUD = [[MBProgressHUD alloc] initWithView:self.view];
	[self.view addSubview:HUD];
	
	HUD.delegate = self;
	HUD.labelText = @"加载中...";
    if ([self respondsToSelector:@selector(getuserinfo)]){
        [HUD showWhileExecuting:@selector(getuserinfo) onTarget:self withObject:nil animated:YES];
    }
}
-(void)getuserinfo
{
    NSString *strsql = [[NSString alloc] initWithFormat:@"SELECT * FROM %@ WHERE %@ >=1 ORDER BY %@,%@ DESC",TABLE_ALONE_SITEPRIZE,ALONE_SITEPRIZE_STATE,ALONE_SITEPRIZE_STATE,ALONE_SITEPRIZE_ID];
    NSMutableArray *array=[sqlDBM selectFromTableWithsql:strsql];
    if ([array count]>=1) {
        for (NSMutableDictionary *dic in array) {
            [self createanticounterfeitData:dic];
        }
    }
}
- (void)createanticounterfeitData:(NSMutableDictionary *)dic
{
    NSMutableArray *demaxiyaHeros;
    [tableviewSections addObject:@""];
    demaxiyaHeros = [[NSMutableArray alloc] init];
    NSString *one,*two,*three,*four,*five,*six;
    one = [dic objectForKey:ALONE_SITEPRIZE_ONE];
    two = [dic objectForKey:ALONE_SITEPRIZE_TWO];
    three = [dic objectForKey:ALONE_SITEPRIZE_THREE];
    four = [dic objectForKey:ALONE_SITEPRIZE_FOUR];
    five = [dic objectForKey:ALONE_SITEPRIZE_FIVE];
    six = [dic objectForKey:ALONE_SITEPRIZE_SIX];
    
    [demaxiyaHeros addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:
                              [dic objectForKey:ALONE_SITEPRIZE_ID],ALONE_SITEPRIZE_ID,
                              [dic objectForKey:ALONE_SITEPRIZE_NAME],ALONE_SITEPRIZE_NAME,
                              [dic objectForKey:ALONE_SITEPRIZE_PERSON],ALONE_SITEPRIZE_PERSON,
                              [dic objectForKey:ALONE_SITEPRIZE_CIRCLE],ALONE_SITEPRIZE_CIRCLE,
                              [[NSString alloc] initWithFormat:@"%d",[one intValue]+[two intValue]+[three intValue]+[four intValue]+[five intValue]+[six intValue]],@"PRIZES",
                              one,ALONE_SITEPRIZE_ONE,
                              two,ALONE_SITEPRIZE_TWO,
                              three,ALONE_SITEPRIZE_THREE,
                              four,ALONE_SITEPRIZE_FOUR,
                              five,ALONE_SITEPRIZE_FIVE,
                              six,ALONE_SITEPRIZE_SIX,
                              nil]];
    [tableviewData addObject:demaxiyaHeros];
    [self.tableview reloadData];
}
-(IBAction)backOff:(id)sender
{
    [self.navigationController popViewControllerAnimated:TRUE];
}

-(IBAction)newSite:(id)sender
{
    AloneSetSiteViewController *setSite = [[AloneSetSiteViewController alloc] init];
    [self.navigationController pushViewController:setSite animated:true];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - 表视图初始化
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[tableviewData objectAtIndex:section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *myCellIdentifier = @"AloneSiteListCell";
    AloneSiteListCell *cell = (AloneSiteListCell *)[tableView dequeueReusableCellWithIdentifier:myCellIdentifier];
    if (cell == nil) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"AloneSiteListCell" owner:nil options:nil];
        for(id currentObject in topLevelObjects)
        {
            if([currentObject isKindOfClass:[AloneSiteListCell class]])
            {
                cell = (AloneSiteListCell *)currentObject;
                break;
            }
        }
        [[cell lbltitle] setText:[[[tableviewData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:ALONE_SITEPRIZE_NAME]];
        [[cell lblpersons] setText:[@"总人数:" stringByAppendingString:[[[tableviewData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:ALONE_SITEPRIZE_PERSON]]];
        [[cell lblcircles] setText:[@"总轮数:" stringByAppendingString:[[[tableviewData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:ALONE_SITEPRIZE_CIRCLE]]];
        [[cell lblprizes] setText:[@"奖品数:" stringByAppendingString:[[[tableviewData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"PRIZES"]]];
        [[cell lblone] setText:[@"一等奖:" stringByAppendingString:[[[tableviewData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:ALONE_SITEPRIZE_ONE]]];
        [[cell lbltwo] setText:[@"二等奖:" stringByAppendingString:[[[tableviewData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:ALONE_SITEPRIZE_TWO]]];
        [[cell lblthree] setText:[@"三等奖:" stringByAppendingString:[[[tableviewData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:ALONE_SITEPRIZE_THREE]]];
        [[cell lblfour] setText:[@"四等奖:" stringByAppendingString:[[[tableviewData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:ALONE_SITEPRIZE_FOUR]]];
        [[cell lblfive] setText:[@"五等奖:" stringByAppendingString:[[[tableviewData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:ALONE_SITEPRIZE_FIVE]]];
        [[cell lblsix] setText:[@"六等奖:" stringByAppendingString:[[[tableviewData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:ALONE_SITEPRIZE_SIX]]];
        [[cell btnEdit]addTarget:self action:@selector(cellclickEdit:) forControlEvents:UIControlEventTouchUpInside];
        [cell btnEdit].tag = indexPath.section;
        [[cell btnDelete]addTarget:self action:@selector(cellclickDelete:) forControlEvents:UIControlEventTouchUpInside];
        [cell btnDelete].tag = indexPath.section;
        UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
        [bgView setBackgroundColor:[UIColor colorWithRed:57.0f/255.0f green:165.0f/255.0f blue:221.0f/255.0f alpha:0.8f]];
        [cell setSelectedBackgroundView:bgView];
    }
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [tableviewSections objectAtIndex:section];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"didSelectRowAtIndexPath");
    selectindex=indexPath;
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSLog(@"%d",[tableviewSections count]);
    return [tableviewSections count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 155;
}

-(IBAction)cellclickEdit:(id)sender
{
    NSInteger intId=[[[[tableviewData objectAtIndex: ((UIButton*)sender).tag] objectAtIndex:0] objectForKey:ALONE_SITEPRIZE_ID] intValue];
    NSLog(@"修改id=%d",intId);
}
-(IBAction)cellclickDelete:(id)sender
{
    NSInteger intId=[[[[tableviewData objectAtIndex: ((UIButton*)sender).tag] objectAtIndex:0] objectForKey:ALONE_SITEPRIZE_ID] intValue];
    NSLog(@"删除id=%d",intId);
}
@end





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS-RATreeView是一个开源的第三方库,提供了多层级的UITableView展示功能。使用该库可以轻松实现多级列表的展开与收起。 首先,在项目中引入iOS-RATreeView库。可以使用CocoaPods引入,也可以手动下载并导入。 接下来,在需要使用多级列表的UIViewController中,创建一个RADataObject类型的数组,用来存储数据。RADataObject是iOS-RATreeView中的一个数据模型,用来表示一条记录。每个RADataObject可以包含多个子RADataObject,从而形成多级列表。 ``` // 创建RADataObject数组 NSMutableArray *data = [NSMutableArray array]; // 创建一级列表 RADataObject *level1_1 = [RADataObject dataObjectWithName:@"Level 1-1" children:nil]; RADataObject *level1_2 = [RADataObject dataObjectWithName:@"Level 1-2" children:nil]; RADataObject *level1_3 = [RADataObject dataObjectWithName:@"Level 1-3" children:nil]; // 创建二级列表 RADataObject *level2_1 = [RADataObject dataObjectWithName:@"Level 2-1" children:nil]; RADataObject *level2_2 = [RADataObject dataObjectWithName:@"Level 2-2" children:nil]; RADataObject *level2_3 = [RADataObject dataObjectWithName:@"Level 2-3" children:nil]; // 将二级列表添加到一级列表中 level1_1.children = @[level2_1, level2_2]; level1_2.children = @[level2_3]; // 将一级列表添加到RADataObject数组中 [data addObject:level1_1]; [data addObject:level1_2]; [data addObject:level1_3]; ``` 创建完数据源后,需要创建RATreeView对象,并设置代理和数据源。 ``` // 创建RATreeView对象 self.treeView = [[RATreeView alloc] initWithFrame:self.view.bounds]; // 设置代理和数据源 self.treeView.delegate = self; self.treeView.dataSource = self; ``` 接下来实现RATreeViewDataSource协议中的方法,用来返回列表的数据。具体实现可以参考下面的代码。 ``` - (UITableViewCell *)treeView:(RATreeView *)treeView cellForItem:(id)item { static NSString *identifier = @"Cell"; UITableViewCell *cell = [treeView dequeueReusableCellWithIdentifier:identifier]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; } // 获取RADataObject对象 RADataObject *dataObject = item; // 设置cell的文本 cell.textLabel.text = dataObject.name; return cell; } - (NSInteger)treeView:(RATreeView *)treeView numberOfChildrenOfItem:(id)item { if (item == nil) { // 如果item为nil,表示请求根节点的子节点数量 return self.data.count; } else { // 获取RADataObject对象 RADataObject *dataObject = item; // 返回子节点数量 return dataObject.children.count; } } - (id)treeView:(RATreeView *)treeView child:(NSInteger)index ofItem:(id)item { if (item == nil) { // 如果item为nil,表示请求根节点的子节点 return self.data[index]; } else { // 获取RADataObject对象 RADataObject *dataObject = item; // 返回子节点 return dataObject.children[index]; } } - (BOOL)treeView:(RATreeView *)treeView canEditRowForItem:(id)item { // 返回是否可以编辑 return YES; } - (void)treeView:(RATreeView *)treeView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowForItem:(id)item { if (editingStyle == UITableViewCellEditingStyleDelete) { // 删除节点 RADataObject *parentObject = [treeView parentForItem:item]; if (parentObject) { NSMutableArray *children = [NSMutableArray arrayWithArray:parentObject.children]; [children removeObject:item]; parentObject.children = children; } else { NSMutableArray *data = [NSMutableArray arrayWithArray:self.data]; [data removeObject:item]; self.data = data; } // 刷新列表 [treeView reloadData]; } } ``` 最后,在RATreeViewDelegate协议中实现展开与收起节点的方法。 ``` - (void)treeView:(RATreeView *)treeView willExpandRowForItem:(id)item { // 获取RADataObject对象 RADataObject *dataObject = item; // 设置节点的展开状态 dataObject.expanded = YES; } - (void)treeView:(RATreeView *)treeView willCollapseRowForItem:(id)item { // 获取RADataObject对象 RADataObject *dataObject = item; // 设置节点的展开状态 dataObject.expanded = NO; } ``` 至此,多级列表展开与收起的功能就实现了。在需要展示多级列表的地方,只需要将创建的RATreeView添加到视图中即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值