一、自定义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
#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
二、绑定数据并显示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