xcode4.2手动创建UITableView

xcode4.2手动创建UITableView

必须实现UITableViewDataSource,UITableViewDelegate  Delegate。

@interface wordpress_otpViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,TSAlertViewDelegate>{

 

UITableView *myTableView;

NSMutableArray *titleList;

}

详细实现:

@implementation xxxViewController


-(void)dealloc{

[myTableView release];
[titleList release];
[mainTitle release];

}


-(void)createTableView{
//导航栏 0, 0, 320, 44
//x1,y1 -> x2,y2
CGRect rect = CGRectMake(0.0f, 0.0f, 320.0f, 460.0f); //CGRectMake(0.0f, 44.0f, 320.0f, 460.0f);
self.myTableView = [[UITableView alloc] initWithFrame:rect];
[myTableView setDelegate:self];
[myTableView setDataSource:self];
[myTableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];

//find all blog in CFG_FILE;
NSMutableDictionary* dict = [ [ NSMutableDictionary alloc ] initWithContentsOfFile:CFG_FILE];
if (dict==nil) {
self.titleList = [NSMutableArray arrayWithObjects: nil];
}else{
//得到词典的数量
int count = [dict count];
NSLog(@"词典的数量为: %d",count);

if (count>0) {
self.titleList = [NSMutableArray arrayWithObjects:nil];
}
//得到词典中所有KEY值
NSEnumerator * enumeratorKey = [dict keyEnumerator];

//快速枚举遍历所有KEY的值
for (NSObject *object in enumeratorKey) {
NSLog(@"遍历KEY的值: %@",object);
[self.titleList addObject:object];
}

}
[self.view addSubview:self.myTableView];

}

#pragma mark - Table view data source
//指定有多少个分区(Section),默认为1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}


//指定每个分区中有多少行,默认为1
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return titleList.count;
}
//绘制Cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
}

// Configure the cell...

cell.imageView.image=[UIImage imageNamed:@"oath_otp.png"];//未选cell时的图片
// cell.imageView.highlightedImage=[UIImage imageNamed:@"bgimg.png"];//选中cell后的图片


cell.textLabel.text =[titleList objectAtIndex: [indexPath row]];

return cell;
}

// Override to support conditional editing of the table view.
//划动cell是否出现del按钮
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
NSLog(@"del....");
return YES;
}


//自定义划动时del按钮内容
- (NSString *)tableView:(UITableView *)tableView
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
return NSLocalizedString(@"DEL",@"");
}

// Override to support editing the table view.
//编辑状态
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source

UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
[self.titleList removeObject:cell.textLabel.text];
NSLog(@"delete data:%@",cell.textLabel.text);
[self deleteBlogByName:cell.textLabel.text];

[tableView reloadData];


// [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
return ;
}

}

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
#pragma mark - Table view delegate
//选中Cell响应事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.

//选中后的反显颜色即刻消失
[tableView deselectRowAtIndexPath:indexPath animated:YES];

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

NSLog(@"%@",cell.textLabel.text);
//加入新的控制器
[self setConfigView:cell.textLabel.text];

}

-(void)setConfigView:(NSString *)blogname{

NSLog(@"blogname:%@",blogname);
configViewController * mconfigViewController = [[configViewController alloc] initWithNibName:@"configViewController" bundle:[NSBundle mainBundle]];
mconfigViewController->m_wpViewController = self;

[mconfigViewController setShakeHandKeyCFGKey:blogname];

[self.navigationController pushViewController:mconfigViewController animated:YES];


[mconfigViewController release];
}

 
| 0个评论