0

在我的应用程序中,我解析了一个 XML 文件,然后我想在 UITableView 中显示该文件的条目。我在网上找到了如何按字母顺序制作部分(如 iPhone 联系人),它适用于我的应用程序。当我点击表格视图中的一行时,我想显示另一个 ViewController,我会在其中找到有关我点击的行的一些信息,但我遇到了一些问题:当我点击一行时,变量 indexPath.row引用了该部分和新视图控制器中的信息不正确。我将在此处发布一些屏幕截图,以显示您想要我试图解释的内容。

在下面的图片中,您可以看到应用程序应该如何工作:

正确的

在以下图片中,您可以看到我的应用程序的错误:

错误

您可以看到在图 1 中名称是相同的,在图 2 中您可以看到名称是错误的。我想这取决于变量indexPath.row。我将在此处发布创建和填充 tableview 的代码:

#import "TableWasteViewController.h"
#import "WasteXmlParser.h"
#import "WasteDetailViewController.h"

@interface TableWasteViewController ()

@property(nonatomic,strong)NSArray *arrayWastes;
@property(nonatomic,strong)NSMutableArray *typeOfWaste;
@property(nonatomic,strong)NSMutableArray *typeOfBin;
@property(nonatomic,strong)NSMutableArray *indexWastes;
@property(nonatomic,strong)NSMutableArray *typeOfWasteBackup;

@end

@implementation TableWasteViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    WasteXmlParser *parser = [[WasteXmlParser alloc]init];
    [parser parseWasteXml];
    self.arrayWastes = [[NSArray alloc]init];
    self.arrayWastes = [parser.arrayWastes mutableCopy];
    self.indexWastes = [[NSMutableArray alloc]init];
    self.typeOfWaste = [[NSMutableArray alloc]init];
    self.typeOfBin = [[NSMutableArray alloc]init];
    for (int i = 0; i < [self.arrayWastes count]; i++) {
        [self.typeOfWaste addObject:[[self.arrayWastes objectAtIndex:i] objectForKey:@"type"]];
        [self.typeOfBin addObject:[[self.arrayWastes objectAtIndex:i]objectForKey:@"place"]];
    }
    for (int i = 0; i < [self.typeOfWaste count]-1; i++) {
        char alphabet = [[self.typeOfWaste objectAtIndex:i] characterAtIndex:0];
        NSString *uniChar = [NSString stringWithFormat:@"%c", alphabet];
        if (![self.indexWastes containsObject:uniChar]) {
            [self.indexWastes addObject:uniChar];
        }
    }
    self.typeOfWasteBackup = [self.typeOfWaste mutableCopy];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.indexWastes count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [self.indexWastes objectAtIndex:section];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *alphabet = [self.indexWastes objectAtIndex:section];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@",alphabet];
    NSArray *wastes = [self.typeOfWaste filteredArrayUsingPredicate:predicate];

    return [wastes count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    UIFont *myFont = [UIFont fontWithName:@"Arial" size:14.0];
    if (cell == nil) {
         cell  = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    NSString *alphabet = [self.indexWastes objectAtIndex:[indexPath section]];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet];
    NSArray *wastes = [self.typeOfWaste filteredArrayUsingPredicate:predicate];

    if ([wastes count] > 0) {
        NSString *cellValue = [wastes objectAtIndex:indexPath.row];
        cell.textLabel.font = myFont;
        cell.textLabel.numberOfLines = 2;
        cell.textLabel.text = cellValue;
    }
    return cell;
}

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

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    NSIndexPath *indexPath = [self.tableWaste indexPathForSelectedRow];
    WasteDetailViewController *vc = segue.destinationViewController;
    vc.typeOfWaste = [self.typeOfWaste objectAtIndex:indexPath.row];
    vc.typeOfBin = [self.typeOfBin objectAtIndex:indexPath.row];
    vc.urlPic = [self.arrayWastes[indexPath.row]objectForKey:@"imgUrl"];
}

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

- (IBAction)backToHome:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end

我希望你能帮助我解决这个问题。谢谢

4

3 回答 3

1

行按节索引,即每个节中的第一项具有indexPath.row == 0. vc.typeOfWaste因此,为了在您的扁平化数组和数组中查找值,您vc.typeOfBin需要按照您的numberOfRowsInSection方法执行一些操作,在该方法中按字母字符过滤扁平化数组,然后使用indexPath.row.

总的来说,这种方法看起来相当混乱,不得不反复过滤你的数据。您的数据结构不能很好地映射到正在解决的问题。我建议使用TLIndexPathTools数据模型TLIndexPathDataModel,因为它是专门为表和集合视图设计的,可以将数据组织成部分,并且可以按索引路径查找项目。如果您愿意,很乐意引导您完成重构。

于 2013-08-29T15:00:13.240 回答
0

几天前我也遇到过这个问题...您需要根据需要设置一个类集属性,然后将该对象添加到一个数组中,然后您可以对一个属性的数组进行排序,然后整个数组将被排序

#import <Foundation/Foundation.h>

@interface HomeFeed : NSObject

@property (nonatomic, copy) UIImage *ItemImage;
@property (nonatomic, copy) NSString *ItemTitle;
@property (nonatomic, copy) NSString *ItemDate;
@property (nonatomic, copy) NSString *ItemDescription;
@property (nonatomic, copy) NSString *ItemHours;
@property (nonatomic, copy) NSString *ItemID;
@property (nonatomic, copy) NSString *itemDetailUrl;
@property (nonatomic, copy) NSString *itemPerson;
@property (nonatomic, copy) NSString *itemThumbUrl;

@property (nonatomic, assign) int ItemDuration;

@end


#import "HomeFeed.h"

@implementation HomeFeed

@synthesize ItemTitle=_ItemTitle, ItemDate=_ItemDate, ItemImage=_ItemImage,ItemID=_ItemID,ItemDuration=_ItemDuration,ItemDescription,ItemHours=_ItemHours,itemDetailUrl,itemPerson,itemThumbUrl;

@end





 NSArray*arr=[responseString JSONValue];
               NSLog(@"Json Dictionary speakersss : %@",arr);
        NSLog(@"Json arr count speaker : %i",arr.count);

        for (int i=0; i<arr.count; i++) {
            NSDictionary *dict=[[ NSDictionary alloc]init];
            dict=[arr objectAtIndex:i];

            HomeFeed *feed = [[HomeFeed alloc] init];


            feed.ItemTitle = [NSString stringWithFormat:@"%@%@%@",[dict objectForKey:@"firstName"],@" ",[dict objectForKey:@"lastName"]];
            feed.ItemDuration = [[NSString stringWithFormat:@"%@", [dict objectForKey:@"count"]] intValue];

            feed.itemDetailUrl=[dict objectForKey:@"detailsUrl"];
            [self.itemsDataArray addObject:feed];
            [itemTitle addObject:feed.ItemTitle];

        }
        HomeFeed *feed = [[HomeFeed alloc] init];

        NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:@"ItemTitle" ascending:YES];
        [self.itemsDataArray sortUsingDescriptors:[NSArray arrayWithObject:sorter]];




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SpeakersCustomCell"];
    cell.selectionStyle = NO;
    cell.accessoryType = NO;

    HomeFeed *feed = [self.itemsDataArray objectAtIndex:indexPath.row];

    UIImageView *arrow = (UIImageView *)[cell viewWithTag:3];

    arrow.image = [UIImage imageNamed:@"accessory.png"];


    UILabel *lblLeft = (UILabel *)[cell viewWithTag:1];

    UILabel *lblRight = (UILabel *)[cell viewWithTag:2];

    lblLeft.text=feed.ItemTitle;




- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{


    HomeFeed *feed=(HomeFeed*)[self.itemsDataArray objectAtIndex:indexPath.row];
    self.onlinDetail.strUrl=feed.itemDetailUrl;

    NSString *key = @"OrientationStringValue";
    NSDictionary *dictionary = [NSDictionary dictionaryWithObject:feed.itemDetailUrl forKey:key];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"NotifSpeaker1" object:nil userInfo:dictionary];

 }
于 2013-08-29T14:56:32.457 回答
0

这是因为您用来将数据传递给WasteDetailViewController 的数组,即“typeOfWaste、typeOfBin 和urlPic”未排序。排序后的数组称为“废物”,但它仅在 numberOfRowsInSection 和 cellForRowAtIndexPath 方法中可用。您需要将废物数组中的数据向前传递,因此不要一直对废物数组进行排序,只需在加载后对其进行一次排序。

添加此属性:

@interface TableWasteViewController ()
@property (strong, nonatomic) NSArray *sortedWastes;
@end

现在在 viewDidLoad

NSString *alphabet = [self.indexWastes objectAtIndex:section];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@",alphabet];
self.sortedWastes = [self.typeOfWaste filteredArrayUsingPredicate:predicate];

最后,在 prepareForSegue

vc.typeOfWaste = [self.sortedWastes objectAtIndex:indexPath.row];

您的问题都源于您正在显示一个排序数组,但是您用来向前传递数据的数组是未排序数组,因此 indexPath 完全没用。

此外,您的 typeOfBin 和 urlPic 也会出错。您需要找到某种方法将所有三个数组链接在一起,以便在对一个数组进行排序时,将它们全部排序。上面的方法只保持你的 typeOfWaste 数组排序。

于 2013-08-29T15:04:00.440 回答