我想在section
中创建一个rows
,其中包含uitableview
cellForRowAtIndexPath:
转换此代码我有很多问题
这是NSString *mytext = [[NSString alloc] initWithFormat:@"%@\r",[[Itemes objectAtIndex:indexPath.row] objectForKey: @"MUSIC"]];
NSString *text = [Itemes objectAtIndex:([indexPath section]*2 + [indexPath row])];
我希望转换为这种风格:
{{1}}
答案 0 :(得分:0)
你试试这个:
NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
NSMutableArray *arrKey = [[NSMutableArray alloc] init];
for(int i=0;i<2;i++){
arrValue = [dic objectForKey:@"Music"];
if(arrValue==nil) {
arrValue = [[NSMutableArray alloc] init];
[dic setObject:arrValue forKey:key];
[arrKey addObject:key];
}
[arrValue addObject:ref];
}
在表格中使用
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[dic objectForKey:[arrKey objectAtIndex:section]] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
[[dic objectForKey:[arrKey objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
}
答案 1 :(得分:0)
试试这个:
当您获得Items数组时,请执行此操作
NSMutableArray *array = [NSMuatbleArray New];
NSMutableDictionary *dict = [NSMutableDictionary New];
for(NSInteger i = 0; i<[Itemes count]/2 ; i+=2) {
{
for(NSInteger j = 0; j<2 ; j++) {
{
NSString *text = [Items objectAtIndex:(i+j)] objectForKey:@"MUSIC"];
[dict setObject:text forKey:[NSString stringWithFormat:@"%d",i]];
}
[array addObject:dict];
}
在Tableview数据源方法
中编辑:在字典setObject中:forKey只将字符串作为键。
-(NSInteger)numberOfsectionsInTableView:(UITableview *)tableview {
return items.count/2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
[[array objectAtIndex:indexPath.row] ObjectForKey:[indexPath.row integerValue]];
}