Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS中的可扩展列表视图_Ios_Objective C_Xcode4.5 - Fatal编程技术网

iOS中的可扩展列表视图

iOS中的可扩展列表视图,ios,objective-c,xcode4.5,Ios,Objective C,Xcode4.5,我需要在我的iOS应用程序中创建可扩展的列表视图,即,如果你点击一个单元格,它应该扩展以提供更多单元格。我将遵循此链接中给出的示例: 这里也给出了实现文件的代码: 但是,在上面的示例中,数组是固定的。我有一个sqlite数据库,其中存储任务及其开始日期等。我需要创建一个可展开的视图,其中不同的行是不同的开始日期,当我点击某个日期时,它应该给出该日期的任务。代码如下: - (void)viewDidLoad { [super viewDidLoad]; allDates = [[N

我需要在我的iOS应用程序中创建可扩展的列表视图,即,如果你点击一个单元格,它应该扩展以提供更多单元格。我将遵循此链接中给出的示例: 这里也给出了实现文件的代码:

但是,在上面的示例中,数组是固定的。我有一个sqlite数据库,其中存储任务及其开始日期等。我需要创建一个可展开的视图,其中不同的行是不同的开始日期,当我点击某个日期时,它应该给出该日期的任务。代码如下:

- (void)viewDidLoad
{
    [super viewDidLoad];
    allDates = [[NSMutableArray alloc]init];

    self.date = [[NSMutableArray alloc] initWithObjects: nil];
   // self.date = [[NSMutableArray alloc]init];
    self.listOfSections_DataSource = [[NSMutableArray alloc]initWithObjects:nil];

    sqlite3_stmt *statement;
    const char *dbpath = [mDatabasePath UTF8String];
    if (sqlite3_open(dbpath,&mDiary)== SQLITE_OK) {
        NSString *selectSQL = [NSString stringWithFormat:@"SELECT * FROM TODO"];
        const char *query_stmt = [selectSQL UTF8String];
        if (sqlite3_prepare_v2(mDiary,
                               query_stmt, -1, &statement, NULL) == SQLITE_OK)
        {
                    while(sqlite3_step(statement) == SQLITE_ROW)
            {
                tempTaskName = [[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 2)];
                [allDates addObject:[[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)]];

                [self.date addObject:tempTaskName];


            }

    }
    sqlite3_finalize(statement);
}
sqlite3_close(mDiary);

NSLog(@"%i",[allDates count]);

        for(int x = 0;x < [allDates count];x++)
        {
            if (sqlite3_open(dbpath,&mDiary)== SQLITE_OK) {
                NSString *selectSQL = [NSString stringWithFormat:@"SELECT * FROM TODO"];
                const char *query_stmt = [selectSQL UTF8String];

                if (sqlite3_prepare_v2(mDiary,
                                       query_stmt, -1, &statement, NULL) == SQLITE_OK){
            temp = [[NSMutableArray alloc]init];
            while(sqlite3_step(statement) == SQLITE_ROW)
            {
                tempTaskName = [[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 2)];
                NSLog(@"%@",tempTaskName);
               // [allDates addObject:[[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)]];
                if([[[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)] isEqualToString:allDates[x]])
                {
                    [self.temp addObject:tempTaskName];
                }




            }
            NSLog(@"task name%@",temp);
            [listOfSections_DataSource addObject:temp];

                } sqlite3_finalize(statement);
            }
            sqlite3_close(mDiary);
        }

    //[self.listOfSections_DataSource addObject:allDates];

   NSMutableArray *emptyArray = [[NSMutableArray alloc]init];
   listOfSections = [[NSMutableArray alloc]init];
    for (int i = 0;i<[allDates count]; i++) {
        [listOfSections addObject:emptyArray];
    }  
    self.selectedSection = -1;
    self.selectedSectionTail = -1;
}
-(void)viewDidLoad
{
[超级视图下载];
allDates=[[NSMutableArray alloc]init];
self.date=[[NSMutableArray alloc]initWithObjects:nil];
//self.date=[[NSMutableArray alloc]init];
self.listOfSections_DataSource=[[NSMutableArray alloc]initWithObjects:nil];
sqlite3_stmt*语句;
const char*dbpath=[mDatabasePath UTF8String];
if(sqlite3\u打开(dbpath,&mdary)==SQLITE\u正常){
NSString*selectSQL=[NSString stringWithFormat:@“从TODO中选择*”;
const char*query_stmt=[selectSQL UTF8String];
如果(sqlite3\u准备\u v2),
查询\u stmt,-1,&语句,NULL)==SQLITE\u OK)
{
while(sqlite3\u步骤(语句)=SQLITE\u行)
{
TestAskname=[[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(语句,2)];
[allDates添加对象:[[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(语句,0)];
[self.date addObject:tentaskname];
}
}
sqlite3_最终确定(声明);
}
sqlite3_关闭(mDiary);
NSLog(@“%i”,[allDates count]);
对于(int x=0;x<[allDates计数];x++)
{
if(sqlite3\u打开(dbpath,&mdary)==SQLITE\u正常){
NSString*selectSQL=[NSString stringWithFormat:@“从TODO中选择*”;
const char*query_stmt=[selectSQL UTF8String];
如果(sqlite3\u准备\u v2),
查询\u stmt,-1,&语句,NULL)==SQLITE\u OK){
temp=[[NSMutableArray alloc]init];
while(sqlite3\u步骤(语句)=SQLITE\u行)
{
TestAskname=[[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(语句,2)];
NSLog(@“%@”,testaskname);
//[allDates添加对象:[[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(语句,0)];
if([[[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(语句,0)]IsequalString:allDates[x]]
{
[self.temp addObject:tentaskname];
}
}
NSLog(@“任务名称%@”,临时);
[listOfSections_数据源addObject:temp];
}sqlite3_最终确定(声明);
}
sqlite3_关闭(mDiary);
}
//[self.listOfSections_DataSource addObject:allDates];
NSMutableArray*emptyArray=[[NSMutableArray alloc]init];
listOfSections=[[NSMutableArray alloc]init];

对于(int i=0;i此示例可能就是您要查找的:


该示例从静态数据中提取,但您应该能够更新它以使用数据库中的动态数据,并提供日期等任务。

我需要的东西现在正在工作,因此我将在这里发布代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    BOOL flag = NO;
    allDates = [[NSMutableArray alloc]init];

    self.date = [[NSMutableArray alloc] init];
   // self.date = [[NSMutableArray alloc]init];
    self.listOfSections_DataSource = [[NSMutableArray alloc]init];

    sqlite3_stmt *statement;
    const char *dbpath = [mDatabasePath UTF8String];
    if (sqlite3_open(dbpath,&mDiary)== SQLITE_OK) {
        NSString *selectSQL = [NSString stringWithFormat:@"SELECT * FROM TODO"];
        const char *query_stmt = [selectSQL UTF8String];
        if (sqlite3_prepare_v2(mDiary,
                               query_stmt, -1, &statement, NULL) == SQLITE_OK)
        {
                    while(sqlite3_step(statement) == SQLITE_ROW)
            {
                tempTaskName = [[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 2)];

                [allDates addObject:[[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)]];

           //[self.date addObject:tempTaskName];


            }

    }
    sqlite3_finalize(statement);
}
    NSLog(@"%i",[allDates count]);
sqlite3_close(mDiary);
   self.date = [NSMutableArray arrayWithCapacity:[allDates count]];
    for(int p = 0; p < [allDates count]; p++)
    {
        for(int q = p+1; q < [allDates count]; q++)
        {
        if([allDates[p] isEqualToString:allDates[q]])
        {
            flag = YES;
            break;

        }
    }
        if(flag)
        {
            //[self.date addObject:allDates[p]];
            [allDates removeObjectAtIndex:p];
        }
        flag = NO;
    }
   // NSLog(@"%u",[self.date count]);
   // [allDates setArray:self.date];
NSLog(@"allDates count%i",[allDates count]);

        for(int x = 0;x < [allDates count];x++)
        {
            if (sqlite3_open(dbpath,&mDiary)== SQLITE_OK) {
                NSString *selectSQL = [NSString stringWithFormat:@"SELECT * FROM TODO"];
                const char *query_stmt = [selectSQL UTF8String];

                if (sqlite3_prepare_v2(mDiary,
                                       query_stmt, -1, &statement, NULL) == SQLITE_OK){
            temp = [[NSMutableArray alloc]init];
            while(sqlite3_step(statement) == SQLITE_ROW)
            {
                tempTaskName = [[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 2)];
                NSLog(@"%@",tempTaskName);

                if([[[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)] isEqualToString:allDates[x]])
                {
                    [self.temp addObject:tempTaskName];
                }




            }
            NSLog(@"task name%@",temp);
            [listOfSections_DataSource addObject:temp];

                } sqlite3_finalize(statement);
            }
            sqlite3_close(mDiary);
        }

    //[self.listOfSections_DataSource addObject:allDates];

   NSMutableArray *emptyArray = [[NSMutableArray alloc]init];
   listOfSections = [[NSMutableArray alloc]init];
    for (int i = 0;i<[allDates count]; i++) {
        [listOfSections addObject:emptyArray];
    }  
    self.selectedSection = -1;
    self.selectedSectionTail = -1;
}
-(void)viewDidLoad
{
[超级视图下载];
布尔标志=否;
allDates=[[NSMutableArray alloc]init];
self.date=[[NSMutableArray alloc]init];
//self.date=[[NSMutableArray alloc]init];
self.listOfSections_DataSource=[[NSMutableArray alloc]init];
sqlite3_stmt*语句;
const char*dbpath=[mDatabasePath UTF8String];
if(sqlite3\u打开(dbpath,&mdary)==SQLITE\u正常){
NSString*selectSQL=[NSString stringWithFormat:@“从TODO中选择*”;
const char*query_stmt=[selectSQL UTF8String];
如果(sqlite3\u准备\u v2),
查询\u stmt,-1,&语句,NULL)==SQLITE\u OK)
{
while(sqlite3\u步骤(语句)=SQLITE\u行)
{
TestAskname=[[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(语句,2)];
[allDates添加对象:[[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(语句,0)];
//[self.date addObject:tentaskname];
}
}
sqlite3_最终确定(声明);
}
NSLog(@“%i”,[allDates count]);
sqlite3_关闭(mDiary);
self.date=[NSMutableArray阵列容量:[所有日期计数]];
对于(int p=0;p<[所有日期计数];p++)
{
对于(int q=p+1;q<[所有日期计数];q++)
{
if([allDates[p]IseQualtString:allDates[q]]))
{
flag=是;
打破
}
}
国际单项体育联合会(旗)
{
//[self.date addObject:allDates[p]];
[所有日期删除对象索引:p];
}
标志=否;
}
//NSLog(@“%u”,[self.date count]);
//[所有日期集合数组:self.date];
NSLog(@“所有日期计数%i,[allDates计数]);
对于(int x=0;x<[allDates计数];x++)
{
if(sqlite3\u打开(dbpath,&mdary)==SQLITE\u正常){
NSString*selectSQL=[NSString stringWithFormat:@“从TODO中选择*”;
const char*query_stmt=[selectSQL UTF8String];
如果(sqlite3\u准备\u v2),
查询\u stmt,-1,&语句,NULL)==SQLITE\u OK){
temp=[[NSMutableArray alloc]init];
while(sqlite3\u步骤(语句)=SQLITE\u行)
{
TestAskname=[[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(语句,2)];
NSLog(@“%@”,testaskname);
if([[[NSString alloc]initWithUTF8String:(const char*)sqlite3\u col
- (void)viewDidLoad
{
    [super viewDidLoad];
    BOOL flag = NO;
    allDates = [[NSMutableArray alloc]init];

    self.date = [[NSMutableArray alloc] init];
   // self.date = [[NSMutableArray alloc]init];
    self.listOfSections_DataSource = [[NSMutableArray alloc]init];

    sqlite3_stmt *statement;
    const char *dbpath = [mDatabasePath UTF8String];
    if (sqlite3_open(dbpath,&mDiary)== SQLITE_OK) {
        NSString *selectSQL = [NSString stringWithFormat:@"SELECT * FROM TODO"];
        const char *query_stmt = [selectSQL UTF8String];
        if (sqlite3_prepare_v2(mDiary,
                               query_stmt, -1, &statement, NULL) == SQLITE_OK)
        {
                    while(sqlite3_step(statement) == SQLITE_ROW)
            {
                tempTaskName = [[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 2)];

                [allDates addObject:[[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)]];

           //[self.date addObject:tempTaskName];


            }

    }
    sqlite3_finalize(statement);
}
    NSLog(@"%i",[allDates count]);
sqlite3_close(mDiary);
   self.date = [NSMutableArray arrayWithCapacity:[allDates count]];
    for(int p = 0; p < [allDates count]; p++)
    {
        for(int q = p+1; q < [allDates count]; q++)
        {
        if([allDates[p] isEqualToString:allDates[q]])
        {
            flag = YES;
            break;

        }
    }
        if(flag)
        {
            //[self.date addObject:allDates[p]];
            [allDates removeObjectAtIndex:p];
        }
        flag = NO;
    }
   // NSLog(@"%u",[self.date count]);
   // [allDates setArray:self.date];
NSLog(@"allDates count%i",[allDates count]);

        for(int x = 0;x < [allDates count];x++)
        {
            if (sqlite3_open(dbpath,&mDiary)== SQLITE_OK) {
                NSString *selectSQL = [NSString stringWithFormat:@"SELECT * FROM TODO"];
                const char *query_stmt = [selectSQL UTF8String];

                if (sqlite3_prepare_v2(mDiary,
                                       query_stmt, -1, &statement, NULL) == SQLITE_OK){
            temp = [[NSMutableArray alloc]init];
            while(sqlite3_step(statement) == SQLITE_ROW)
            {
                tempTaskName = [[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 2)];
                NSLog(@"%@",tempTaskName);

                if([[[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)] isEqualToString:allDates[x]])
                {
                    [self.temp addObject:tempTaskName];
                }




            }
            NSLog(@"task name%@",temp);
            [listOfSections_DataSource addObject:temp];

                } sqlite3_finalize(statement);
            }
            sqlite3_close(mDiary);
        }

    //[self.listOfSections_DataSource addObject:allDates];

   NSMutableArray *emptyArray = [[NSMutableArray alloc]init];
   listOfSections = [[NSMutableArray alloc]init];
    for (int i = 0;i<[allDates count]; i++) {
        [listOfSections addObject:emptyArray];
    }  
    self.selectedSection = -1;
    self.selectedSectionTail = -1;
}
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {


        return [listOfSections_DataSource count];


    }


    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {


          if(selectedSection !=section)
            return 1;
        else{
            NSArray *array = [listOfSections objectAtIndex:section];

            return [array count]; 
        } 


}



    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

        for(int y = 0;y<[allDates count]; y++)
        {
            if (section == y) {
                return allDates[y];
            }
        }
    }
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        static NSString *CellIdentifier = @"cell";

        if(self.selectedSection == indexPath.section){//this is just to check to see if this section is the one we touched

            UITableViewCell *cell =  [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            if (cell == nil)
            {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            }

          //  cell.textLabel.text = [[listOfSections_DataSource objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
             NSArray *myArray = [listOfSections_DataSource objectAtIndex:indexPath.section];//tasks
            NSString* myString = [myArray objectAtIndex:indexPath.row]; // get task for that row, under the date

            cell.textLabel.text = myString; 
            return cell;
        }
        else{//THIS IS going to happen the first time
            UITableViewCell *cell =  [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            if (cell == nil)
            {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            }

                cell.textLabel.text = @"more";

            return cell;
        }
    }

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


        UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];

        //check to see if the cell is for exapnding or for selection
        if([cell.textLabel.text isEqualToString:@"more"]){ // this means we need to expand

            listOfSections = [[NSMutableArray alloc]init];
            //setting up the list of section with empty arrays

            NSMutableArray *emptyArray = [[NSMutableArray alloc]init];
            for (int i = 0;i<[allDates count]; i++) {
                [listOfSections addObject:emptyArray];
            }

            //Add array of tasks here
            [listOfSections replaceObjectAtIndex:indexPath.section withObject:[listOfSections_DataSource objectAtIndex:indexPath.section]];


            int tapedRow = [indexPath section];

            self.selectedSection = tapedRow;

            NSMutableIndexSet *myIndexSet = [[NSMutableIndexSet alloc]init];
            [myIndexSet addIndex:selectedSection];
            [myIndexSet addIndex:selectedSectionTail];

            // Updating section in the tableview
            if(selectedSectionTail!=-1)
                [taskTable reloadSections:(NSIndexSet*)myIndexSet withRowAnimation:UITableViewRowAnimationFade];
            else {
                [taskTable reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:
                 UITableViewRowAnimationFade];
            }
            //[taskTable reloadData];
        }
        else{
        }
        selectedSectionTail = selectedSection;
    }
struct CellData {
    var isOpened = Bool()
    var title = String()
    var sectionData = [String]()
}

class TableViewController: UITableViewController {
    var tableViewData = [CellData]()
    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem

        tableViewData = [CellData(isOpened: false, title: "title1", sectionData: ["cell1","cell2","cell3"]),CellData(isOpened: false, title: "title2", sectionData: ["cell1","cell2","cell3"]),CellData(isOpened: false, title: "title3", sectionData: ["cell1","cell2","cell3"])]
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return tableViewData.count
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        if tableViewData[section].isOpened {
            return tableViewData[section].sectionData.count + 1
        }
        return 1
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row == 0 {
            guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell") else {
                return UITableViewCell()
            }
            cell.textLabel?.text = tableViewData[indexPath.section].title
            return cell
        } else {
            guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell") else {
                return UITableViewCell()
            }
            cell.textLabel?.text = tableViewData[indexPath.section].sectionData[indexPath.row - 1]
            return cell
        }
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row == 0 {
            if tableViewData[indexPath.section].isOpened {
                tableViewData[indexPath.section].isOpened = false
            }else {
                tableViewData[indexPath.section].isOpened = true
            }
            let sections = IndexSet(integer: indexPath.section)
            tableView.reloadSections(sections, with: .none)
        }
    }
}