【问题标题】:How to generate different data on row from arrays in different sections of uitableview cell如何从uitableview单元格不同部分的数组中生成不同的行数据
【发布时间】:2009-06-08 10:01:49
【问题描述】:

我有一些数据库,我正在生成类似这样的数据。

首先从数据库中获取 categoryid 和 categoryName。表的sectiontitle是categoryName。

这里是一些示例代码

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return categoryNamesList.count;
}

-(NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [categoryNamesList objectAtIndex:section];   

}

我想根据类别 id 生成 flashcardid 和 flashcard 名称。 categoryid 存储在数据库中,如 1,2,3。

所以我在查询中传递了 categoryid:

select flashCardId,flashCardName from flashCardInfo where categoryId=(parameter)

在 flashcardid 中,我得到 1、2、3,在 flashcards 名称数组中,我第一次得到 flashcard1、flashcard2、flashcard3 现在我想在我的单元格上生成文本,例如第 1 部分的 firstcategoryname,然后在上生成相关的抽认卡名称然后在第二部分第二个类别名称和行上的相关抽认卡名称上的行。在哪里增加类别 ID 以便我获得下一个名称和 ID 以及如何在单元格上设置文本

现在我如何在

上生成它

这里是代码;它在indexviewcontroller.m 文件中。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return categoryNamesList.count;
}


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

    return flashCardsNamesList.count;       

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";  
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
if (cell == nil) {      
    cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];      
}


    if(categoryID<listOfCategoryId.count)
    {   
        for(int i=0;i<flashCardsNamesList.count;i++)
        {
        [cell.FlashCardsNames setText:[flashCardsNamesList objectAtIndex:i]];
        }       
    categoryID++;
    [self getFlashCard:categoryID];
    }           
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [indexTableView deselectRowAtIndexPath:indexPath animated:YES];

    for(int i=0;i<flashCardsId.count;i++)
    {
            questionViewControllerObj = [[flashCardQuestionViewController alloc] initWithNibName:@"FlashCardQuestionView" bundle:[NSBundle mainBundle]];
            [self.navigationController pushViewController:questionViewControllerObj animated:YES];                  
            questionViewControllerObj.flashcardid = [[flashCardsId objectAtIndex:i] intValue];
            questionViewControllerObj.categoryid = [[listOfCategoryId objectAtIndex:indexPath.section]intValue];
    }

}


/*
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {
    }
    if (editingStyle == UITableViewCellEditingStyleInsert) {
    }
}
*/
/*
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/
/*
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/
/*
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

-(NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [categoryNamesList objectAtIndex:section];   

}

# pragma mark -
# pragma mark viewDidLoad

- (void)viewDidLoad {
    [super viewDidLoad];    
    [self getIntialData];
    self.navigationItem.title = @"Index";
    indexTableView.backgroundColor = [UIColor lightGrayColor];  
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
}

- (void)viewDidDisappear:(BOOL)animated {
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark -
#pragma mark Database Methods


-(void)getIntialData
{
    [self getcategory];
    [self getFlashCard:categoryID]; 

}
  //getting categoryid,categoryname from the database
-(void)getcategory
{
    if(listOfCategoryId!=nil)
        [listOfCategoryId removeAllObjects];
    else
        listOfCategoryId = [[NSMutableArray alloc] init];

    if(categoryNamesList!=nil)
        [categoryNamesList removeAllObjects];
    else
        categoryNamesList = [[NSMutableArray alloc] init];

    if(listOfCategoryType!=nil)
        [listOfCategoryType removeAllObjects];
    else
        listOfCategoryType = [[NSMutableArray alloc] init];

    clsDatabase *clsDatabaseObject = [[clsDatabase alloc] init];
    sqlite3_stmt *dataRows = [clsDatabaseObject getDataset:"select categoryId,categoryName,categoryType from cardCategoryInfo"];
    while(sqlite3_step(dataRows) == SQLITE_ROW)
    {       
        [listOfCategoryId addObject:[NSNumber numberWithInt:sqlite3_column_int(dataRows,0)]];
        [categoryNamesList addObject: [NSString stringWithUTF8String:(char *)sqlite3_column_text(dataRows,1)]] ;
        [listOfCategoryType addObject:[NSNumber numberWithInt:sqlite3_column_int(dataRows,2)]];
    }
    sqlite3_finalize(dataRows);
    [clsDatabaseObject release];    
    categoryID = [[listOfCategoryId objectAtIndex:categoryID] intValue];
}


   //getting flashcardId and flashcard name from categoryid
-(void)getFlashCard:(int)categoryid
{
    if(flashCardsId!=nil)
        [flashCardsId removeAllObjects];
    else
        flashCardsId = [[NSMutableArray alloc] init];

    if(flashCardsNamesList!=nil)
        [flashCardsNamesList removeAllObjects];
    else
        flashCardsNamesList = [[NSMutableArray alloc] init];



    NSString *martialStr=[NSString stringWithFormat:@"%d", categoryid];

    NSString *queryStr=[[NSString alloc] initWithString:@"select flashCardId,flashCardName from flashCardInfo where categoryId="];
    queryStr=[queryStr stringByAppendingString:martialStr];


    unsigned int lengthOfString=[queryStr length];
    char temp2[lengthOfString +1];
    strcpy(temp2, [queryStr cStringUsingEncoding:NSUTF8StringEncoding]);    
    clsDatabase *clsDatabaseObject = [[clsDatabase alloc] init];    
    sqlite3_stmt *dataRows = [clsDatabaseObject getDataset:temp2];
    while(sqlite3_step(dataRows) == SQLITE_ROW)
    {       
        [flashCardsId addObject:[NSNumber numberWithInt:sqlite3_column_int(dataRows,0)]];       
        [flashCardsNamesList addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(dataRows,1)]];    

    }
    sqlite3_finalize(dataRows);
    [clsDatabaseObject release];

}

- (void)dealloc {
    [self.navigationController popViewControllerAnimated:YES];
    [listOfCategoryId release];
    [listOfCategoryType release];
    [questionViewControllerObj release];
    [categoryNamesList release];
    [flashCardsNamesList release];
    [flashCardsId release];
    [indexTableView release];   
    [super dealloc];
}



@end

【问题讨论】:

    标签: objective-c iphone xcode


    【解决方案1】:

    您需要实现 cellForRowAtIndexPath 来填充每个表格部分中的行。

    例如

    - (UITableViewCell *)tableView:(UITableView *)tableView
        cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
      // This method gets called for each section and row in your table
      NSUInteger cellRow = [indexPath row];
      NSUInteger cellSection = [indexPath section];
    
      // Create new table cell
      // Deliberately ignoring reusing cells to keep this example simple
      UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"MyIdentifier"] autorelease];
    
      // Now build up a string with the info you want in it based on
      // data in cellSection and cellRow, and set the cell text
      cell.text = @"put your text here";
    

    更新:如果您希望有人为您编写所有代码,您需要编辑您的帖子以使其更短、更清晰。只需解释您拥有哪些数据结构,以及表格应该是什么样子,例如

    DATA STRUCTURES 
    
    -NSDictionary of section names as keys. 
    -Each NSDictionary key points at an NSArray, which holds NSStrings that need to go in each row. 
    
    DESIRED OUTPUT 
    
    section 0 heading 
    row 0 
    row 1 
    
    section 1 heading 
    row 0 
    row 1 
    

    【讨论】:

    • 如果您在代码中定义示例数组会有所帮助。我已更新我的回复以请求更多信息。
    • 我想要和你上面写的一样的东西。我正在用我的代码编辑问题,为你提供详细的信息
    【解决方案2】:

    重复使用单元格

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc]  initWithFrame:CGRectZero reuseIdentifier:@"MyIdentifier"] autorelease];
    }
    // Configure the cell
    ...
    return cell;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-16
      • 1970-01-01
      相关资源
      最近更新 更多