简体   繁体   English

UItableView来自Plist的数据

[英]UItableView data from Plist

I have a plist with thousands of Array into it.and each array contains Dictionary with 51 items in it.thats means total of some 40,000 data,I have populated into the UITableView correctly .IF I enter around 10 Array into the UITableView ..its display the rows in good view...but when I go beyond like 40 arrays ..the UITableView gets weird.you can't even understand. 我有成千上万阵列的一个的plist成it.and每个阵列包含字典与it.thats 51项表示总一些40000的数据,我已填充入UITableView .IF我进入大约10正确ArrayUITableView ..its以良好的视图显示行...但是当我超过40个数组时.. UITableView变得很奇怪。您甚至无法理解。 第一 . .

and UITableView is below.. UITableView在下面。

秒 .

I have also tried to Exapanded rows for each section.that also not working for me ..what should I do? 我还尝试过为每个部分扩展行。这对我也不起作用..我该怎么办?

EDIT code 编辑代码

-(void)ViewDidLoad
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"sam" ofType:@"plist"];

//Load the file in a dictionnary
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];

self.myData = dict;

//SORTING THE DICTIONARY    
NSArray *dicoArray = [[self.myData allKeys] sortedArrayUsingComparator:^(id firstObject, id secondObject) {
    return [((NSString *)firstObject) compare:((NSString *)secondObject) options: NSCaseInsensitiveSearch];
}];

self.mySections = dicoArray;

NSLog(@"path values%@",tableDict);


}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{



return [self.mySections count];



}

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

 if ([self tableView:tableView canCollapseSection:section])
 {
    if ([expandedSections containsIndex:section])
    {

NSString *key = [self.mySections objectAtIndex:section];
NSArray *dataInSection = [[self.myData objectForKey:key] objectAtIndex:0];
return [dataInSection count];
    }
    return 1;
  }
return 1;
  }


-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSString *key = [self.mySections objectAtIndex:section];
 return [NSString stringWithFormat:@"%@", key];
}


-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return self.mySections;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

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

// Configure the cell...

NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];



NSString *key = [self.mySections objectAtIndex:section];

NSDictionary *dataForSection = [[self.myData objectForKey:key] objectAtIndex:0];
NSArray *array=dataForSection.allKeys;

cell.textLabel.text = [[dataForSection allKeys] objectAtIndex:row];    
cell.detailTextLabel.text=[dataForSection valueForKey:[array objectAtIndex:indexPath.row]];

   return cell;     
 }

this code works fine If I take less Array say 10Array with 51 values into it...but when I take more...screen shows like this 这段代码可以正常工作如果我少用Array,说10Array带有51个值...但是当我多用...时,屏幕显示如下

edit 编辑

 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
 <plist version="1.0">
 <dict>
    <key>BUTTER,WITH SALT</key>
    <array>
        <dict>
            <key>Alpha_Carot_(µg)</key>
            <string>0</string>
            <key>Ash_(g)</key>
            <string>2.11</string>
            <key>Beta_Carot_(µg)</key>
            <string>158</string>
            <key>Beta_Crypt_(µg)</key>
            <string>0</string>
            <key>Calcium_(mg)</key>
            <string>24</string>
            <key>Carbohydrt_(g)</key>
            <string>0.06</string>
            <key>Cholestrl_(mg)</key>
            <string>215</string>
            <key>Choline_Tot_ (mg)</key>
            <string>18.8</string>
            <key>Copper_mg)</key>
            <string>0</string>
            <key>Energ_Kcal</key>
            <string>717</string>
            <key>FA_Mono_(g)</key>
            <string>21.021</string>
            <key>FA_Poly_(g)</key>
            <string>3.043</string>
            <key>FA_Sat_(g)</key>
            <string>51.368</string>
            <key>Fiber_TD_(g)</key>
            <string>0</string>
            <key>Folate_DFE_(µg)</key>
            <string>3</string>
            <key>Folate_Tot_(µg)</key>
            <string>3</string>
            <key>Folic_Acid_(µg)</key>
            <string>0</string>
            <key>Food_Folate_(µg)</key>
            <string>3</string>
            <key>GmWt_1</key>
            <string>227</string>
            <key>GmWt_2</key>
            <string>14.2</string>
            <key>GmWt_Desc1</key>
            <string>1 cup</string>
            <key>GmWt_Desc2</key>
            <string>1 tbsp</string>
            <key>Iron_(mg)</key>
            <string>0.02</string>
            <key>Lipid_Tot_(g)</key>
            <string>81.11</string>
            <key>Lut+Zea_ (µg)</key>
            <string>0</string>
            <key>Lycopene_(µg)</key>
            <string>0</string>
            <key>Magnesium_(mg)</key>
            <string>2</string>
            <key>Manganese_(mg)</key>
            <string>0</string>
            <key>Niacin_(mg)</key>
            <string>0.042</string>
            <key>Panto_Acid_mg)</key>
            <string>0.11</string>
            <key>Phosphorus_(mg)</key>
            <string>24</string>
            <key>Potassium_(mg)</key>
            <string>24</string>
            <key>Protein_(g)</key>
            <string>0.85</string>
            <key>Refuse_Pct</key>
            <string>0</string>
            <key>Retinol_(µg)</key>
            <string>671</string>
            <key>Riboflavin_(mg)</key>
            <string>0.034</string>
            <key>Selenium_(µg)</key>
            <string>1</string>
            <key>Sodium_(mg)</key>
            <string>714</string>
            <key>Sugar_Tot_(g)</key>
            <string>0.06</string>

        </dict>

    </array>
    <key>BUTTER,WHIPPED,WITH SALT</key>
    <array>
        <dict>
            <key>Alpha_Carot_(µg)</key>
            <string>0</string>
            <key>Ash_(g)</key>
            <string>2.11</string>
            <key>Beta_Carot_(µg)</key>
            <string>158</string>
            <key>Beta_Crypt_(µg)</key>
            <string>0</string>
            <key>Calcium_(mg)</key>
            <string>24</string>
            <key>Carbohydrt_(g)</key>
            <string>0.06</string>
            <key>Cholestrl_(mg)</key>
            <string>219</string>
            <key>Choline_Tot_ (mg)</key>
            <string>18.8</string>
            <key>Copper_mg)</key>
            <string>0.016</string>
            <key>Energ_Kcal</key>
            <string>717</string>
            <key>FA_Mono_(g)</key>
            <string>23.426</string>
            <key>FA_Poly_(g)</key>
            <string>3.012</string>
            <key>FA_Sat_(g)</key>
            <string>50.489</string>
            <key>Fiber_TD_(g)</key>
            <string>0</string>
            </dict></array>
      </dict></plist>

I now see what the problem is. 我现在看到了问题所在。 You have an index selection that is way too big. 您选择的索引太大了。

You can remove this by removing the sectionIndexTitlesForTableView method. 您可以通过删除sectionIndexTitlesForTableView方法来删除它。

If you really want this, you can format all the strings to not be bigger than a few characters like: 如果确实要这样做,则可以将所有字符串设置为不超过几个字符的格式,例如:

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    NSMutableArray *shorterArray = [[NSMutableArray alloc]init];
    for( NSString *sectionTitle in self.mySections ){
        NSString *shortSectionTitle;
        if( sectionTitle.length > 4 ){
            shortSectionTitle = [NSString stringWithFormat:@"%@..", [sectionTitle substringToIndex:4]];
        } else {
            shortSectionTitle = sectionTitle;
        }
        [shorterArray addObject:shortSectionTitle];
    }
    return shorterArray;
}

If you need more help, let me know 如果您需要更多帮助,请告诉我

NSDictionary *dataForSection = [self.mySections objectAtIndex:section];
NSArray *array=dataForSection.allKeys;

cell.textLabel.text = [array objectAtIndex:row];    
cell.detailTextLabel.text=[dataForSection objectForKey:[array objectAtIndex:row]];

Try removing if (cell == nil) { } condition. 尝试删除if(cell == nil){}条件。 That is declare, cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease]; 即声明,cell = [[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 redirectIdentifier:CellIdentifier] autorelease]; outside if brackets. 如果在括号之外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM