i am using a function to fill dictionary in a array here is the code
我正在使用一个函数来填充数组中的字典这里是代码
-(void)getAllFlashCardsNames
{
if ([listofitems count]==0)
listofitems = [[NSMutableArray alloc] init];
else
[listofitems removeAllObjects];
for(int i=0;i<listOfCategoryId.count;i++)
{
int j=[[listOfCategoryId objectAtIndex:i]intValue];
[self getFlashCard:j];
NSArray *flashCardsNames = flashCardsNamesList;
NSArray *flashCardsids = flashCardsId;
NSLog(@"FLash Card Ids %@",flashCardsids);
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:flashCardsNames,@"flashCards",flashCardsids,@"flashCardId",nil];
[listofitems addObject:dictionary];
}
}
in the above code the array flashcardsNamesList,flashCardsId changes everytime when calling the function [self getFlashCard:j]; j is a parameter to change categoryid which comes from the listOfCategoryId array..
在上面的代码中数组flashcardsNamesList,flashCardsId每次调用函数时都会改变[self getFlashCard:j]; j是一个更改categoryid的参数,它来自listOfCategoryId数组。
now how do i retrieve values from the dictionary i want to show different flashcardsNames on different sections in uitableview.
现在我如何从字典中检索值,我想在uitableview中的不同部分显示不同的flashcardsNames。
here is the code i m using to retrieve values
这是我用来检索值的代码
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [listofitems count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section {
NSDictionary *dictionary =[listofitems objectAtIndex:section];
NSLog(@"dictionary=%@",dictionary);
NSArray *array =[dictionary objectForKey:@"flashCards"];
NSLog(@"array=%@",array);
NSLog(@"Section Count = %d",array.count);
return array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
CustomCell *cell = (CustomCell *)[tableViewdequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
NSDictionary *dictionary =[listofitems objectAtIndex:indexPath.section];
NSArray *array =[dictionary objectForKey:@"flashCards"];
NSArray *array1=[dictionary objectForKey:@"flashCardId"];
NSString *cellValue=[array objectAtIndex:indexPath.row];
NSString *cellValue1=[array1 objectAtIndex:indexPath.row];
[cell.FlashCardsNames setText:cellValue];
[cell setFlashCardId:[cellValue1 intValue]];
return cell;
}
but the method - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
does not get called
但是方法 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath没有被调用
1 个解决方案
#1
but the method
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
does not called但方法 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath未调用
Have you set the object that your method is implemented in as the data source of your table view? UITableView
hands some of the work off to another object, which must conform to the UITableViewDataSource
and UITableViewDelegate
protocols; you must then set the object as the dataSource
and delegate
of the table view, either in IB or programmatically (the data source and delegate can be different objects, but are commonly the same object). Take a look at this article which explains more about it; once this has been done, your object must handle the tableView:cellForRowAtIndexPath:
and tableView:numberOfRowsInSection:
methods, which will be called on your object by the table view.
您是否已将实现方法的对象设置为表视图的数据源? UITableView将一些工作交给另一个对象,该对象必须符合UITableViewDataSource和UITableViewDelegate协议;然后,您必须将对象设置为表视图的dataSource和委托,可以在IB中或以编程方式设置(数据源和委托可以是不同的对象,但通常是相同的对象)。看看这篇文章,它解释了更多关于它的内容;完成此操作后,您的对象必须处理tableView:cellForRowAtIndexPath:和tableView:numberOfRowsInSection:方法,这些方法将通过表视图在您的对象上调用。
Also, the lines:
还有,行:
if ([listofitems count]==0)
listofitems = [[NSMutableArray alloc] init];
do not make sense. I assume you are checking whether the array has been allocated or not, and if not, to allocate it. If the array hasn't been allocated, it will be nil
, so sending count
to it will have no effect anyway. If it has been allocated previously, but deallocated but not reverted back to nil
it will be a bad pointer and cause your application to crash.
没有意义。我假设您正在检查数组是否已分配,如果没有,则分配它。如果尚未分配数组,则它将为nil,因此向其发送计数无论如何都不会产生任何影响。如果先前已经分配了,但是已经解除分配但没有恢复为nil,则它将是一个错误的指针并导致应用程序崩溃。
A better way to allocate it would be to do so in your class's awakeFromNib
method, or applicationDidFinishLaunching:
method, if you are implementing this in your UIApplicationDelegate
subclass. Don't forget to release it in your dealloc
method.
分配它的更好方法是在类的awakeFromNib方法或applicationDidFinishLaunching:方法中执行此操作,如果要在UIApplicationDelegate子类中实现此方法。不要忘记在dealloc方法中释放它。
#1
but the method
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
does not called但方法 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath未调用
Have you set the object that your method is implemented in as the data source of your table view? UITableView
hands some of the work off to another object, which must conform to the UITableViewDataSource
and UITableViewDelegate
protocols; you must then set the object as the dataSource
and delegate
of the table view, either in IB or programmatically (the data source and delegate can be different objects, but are commonly the same object). Take a look at this article which explains more about it; once this has been done, your object must handle the tableView:cellForRowAtIndexPath:
and tableView:numberOfRowsInSection:
methods, which will be called on your object by the table view.
您是否已将实现方法的对象设置为表视图的数据源? UITableView将一些工作交给另一个对象,该对象必须符合UITableViewDataSource和UITableViewDelegate协议;然后,您必须将对象设置为表视图的dataSource和委托,可以在IB中或以编程方式设置(数据源和委托可以是不同的对象,但通常是相同的对象)。看看这篇文章,它解释了更多关于它的内容;完成此操作后,您的对象必须处理tableView:cellForRowAtIndexPath:和tableView:numberOfRowsInSection:方法,这些方法将通过表视图在您的对象上调用。
Also, the lines:
还有,行:
if ([listofitems count]==0)
listofitems = [[NSMutableArray alloc] init];
do not make sense. I assume you are checking whether the array has been allocated or not, and if not, to allocate it. If the array hasn't been allocated, it will be nil
, so sending count
to it will have no effect anyway. If it has been allocated previously, but deallocated but not reverted back to nil
it will be a bad pointer and cause your application to crash.
没有意义。我假设您正在检查数组是否已分配,如果没有,则分配它。如果尚未分配数组,则它将为nil,因此向其发送计数无论如何都不会产生任何影响。如果先前已经分配了,但是已经解除分配但没有恢复为nil,则它将是一个错误的指针并导致应用程序崩溃。
A better way to allocate it would be to do so in your class's awakeFromNib
method, or applicationDidFinishLaunching:
method, if you are implementing this in your UIApplicationDelegate
subclass. Don't forget to release it in your dealloc
method.
分配它的更好方法是在类的awakeFromNib方法或applicationDidFinishLaunching:方法中执行此操作,如果要在UIApplicationDelegate子类中实现此方法。不要忘记在dealloc方法中释放它。