如何在一个UIView中显示一个UITableView

iOS 码拜 9年前 (2015-05-09) 670次浏览 0个评论
 

   UIView中有标签, 有toolbar,另外还想显示一个表,不是基于导艏的,请问如何在该 UIView视图上显示一个UITableView呢?而且可以对表进行删除行操作

20分
在UIView中添加表格
1、直接在Interface Builder中拖一个来就好了,然后绑定委托,数据
2、代码创建:

     self.view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)] autorelease];
     UITableView * table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 100, 200)  style:UITableViewStylePlain];
//可编辑
table.allowsSelectionDuringEditing = YES;
     [self.view addSubview:table];

     

5分
楼上是对的,可以设置UITableiew的样式以及可以设置可选和删除
5分
2楼对的!
10分
– (BOOL)tableView:(UITableView *)tableView 
canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
– (void)tableView:(UITableView *)tableView 
moveRowAtIndexPath:(NSIndexPath *)fromIndexPath 
      toIndexPath:(NSIndexPath *)toIndexPath {
    NSUInteger fromRow = [fromIndexPath row];
    NSUInteger toRow = [toIndexPath row];
    
NSLog(@”%@”, arr);

   id object = [[arr objectAtIndex:fromRow] retain];

    [arr removeObjectAtIndex:fromRow];
    [arr insertObject:object atIndex:toRow];
   [object release];
}
– (void)tableView:(UITableView *)tableView 
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
forRowAtIndexPath:(NSIndexPath *)indexPath {

NSUInteger row = [indexPath row];
NSArray *a = [arr objectAtIndex:row];
  

    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *path=[paths objectAtIndex:0];
path=[path stringByAppendingPathComponent:@”mydata.db”];

if (sqlite3_open( [path UTF8String], &mydatabase)!=SQLITE_OK) {
sqlite3_close(mydatabase);
NSAssert(0,@”failed to open database”);
}
char *errmsg;
char *name = [[a objectAtIndex:0] UTF8String];
NSString *sqlcmd=[[NSString alloc]initWithFormat: @”delete from  student  where name=””%s””;”,name];

if (sqlite3_exec(mydatabase,[sqlcmd UTF8String], NULL,NULL, &errmsg)!=SQLITE_OK) {
sqlite3_close(mydatabase);
NSAssert1(0,@”error update table %s”,errmsg);
}

   [arr removeObjectAtIndex:row];

    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
                     withRowAnimation:UITableViewRowAnimationFade];
}
– (void)tableView:(UITableView *)tableView 
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

NSUInteger row = [indexPath row];
NSArray *a = [arr objectAtIndex:row];
Rework *r = [[Rework alloc] init];
[self.navigationController pushViewController:r animated:YES];

[r.name setText:[a objectAtIndex:0]];
[r.age setText:[NSString stringWithFormat:@”%@”,[a objectAtIndex:1]]];
[r.number setText:[a objectAtIndex:2]];

[arr removeObjectAtIndex:row];
[r release];

}

IB 里view上加个 tableview ,再绑定 datasource 和delegate。。。代码里实现下就ok了呀
要实现删除单元行,实现
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明如何在一个UIView中显示一个UITableView
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!