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

解决方案 »

  1.   

    在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];     
      

  2.   

    楼上是对的,可以设置UITableiew的样式以及可以设置可选和删除
      

  3.   

    - (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];

    }
      

  4.   

    IB 里view上加个 tableview ,再绑定 datasource 和delegate代码里实现下就ok了呀
      

  5.   

    要实现删除单元行,实现
    -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {