簡體   English   中英

iOS中的可擴展列表視圖

[英]expandable list view in iOS

我需要在我的iOS應用程序中創建可擴展的列表視圖,即。 如果你點擊一個單元格,它應該擴展以提供更多的單元格。 我正在按照此鏈接給出的示例: 切換顯示/隱藏子表格單元iOS此處也給出了實現文件的代碼: http//pastie.org/7756763

但是,在上面的示例中,數組是固定的。 我有一個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;
}

表視圖方法是:

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

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

        NSArray *myArray = [listOfSections_DataSource objectAtIndex:indexPath.section];//this now contains your cities
        NSString* myString = [myArray objectAtIndex:indexPath.row]; // get city for that row, under the state

        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;
}

這里的問題是:1。屏幕上顯示的部分數量是正確的,但是當點擊第一個單元格時,它會消失。 2.當點擊任何單元格時,列表不會擴展。

請有人幫幫我嗎? 謝謝..

您可以找到以下示例: http//developer.apple.com/library/ios/#samplecode/TableViewUpdates/Introduction/Intro.html

樣本來自靜態數據,但您應該能夠更新它以使用來自數據庫的動態數據,並且還可以為日期等提供任務。

JKExpandTableView是一個實現可擴展表視圖(UITableView的子類)的開源庫,應該可以幫到你: http//jackkwok.github.io/JKExpandTableView/

查看示例應用。

截圖

我需要的東西現在正在工作,所以我要在這里發布代碼:

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

我知道必須有更好的方法來做到這一點,但這個解決方案對我有用,所以我決定發布它。

在Swift 4中:

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)
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM