在我的应用程序中,我解析了一个XML文件,然后我想在UITableView中显示该文件的条目。我在web上找到了如何按字母顺序排列部分(比如iPhone联系人),它适用于我的应用程序。当我点击我的tableview中的一行时,我想显示另一个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
我希望你能帮助我解决这个问题。谢谢
答案 0 :(得分:1)
行按部分编制索引,即每个部分中的第一项具有indexPath.row == 0
。因此,为了在展平的vc.typeOfWaste
和vc.typeOfBin
数组中查找值,您需要在numberOfRowsInSection
方法的某些方面执行某些操作,在此方法中筛选展平的数组按字母字符,然后使用indexPath.row
获取已过滤数组的项目。
总体而言,这种方法看起来相当混乱,不得不反复过滤您的数据。您的数据结构无法很好地映射到正在解决的问题。我建议使用TLIndexPathTools数据模型TLIndexPathDataModel
,因为它专门用于表和集合视图,可以将数据组织成各个部分,并可以按索引路径查找项目。如果你愿意,很乐意引导你完成一个重构。
答案 1 :(得分: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];
}
答案 2 :(得分:0)
这是因为您用于将数据传递给WasteDetailViewController的数组(“typeOfWaste,typeOfBin和urlPic”)未排序。已排序的数组称为“wastes”,但它仅在numberOfRowsInSection和cellForRowAtIndexPath方法中可用。你需要向前传递wastes数组中的数据,所以不要一直对wastes数组进行排序,只需在加载后对其进行排序即可。
添加此属性:
@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数组进行排序。