[英]UITableView and alphabetical order
在我的应用程序中,我解析一个XML文件,然后在UITableView中显示此文件的条目。 我在网络上发现了如何按字母顺序(例如iPhone通讯录)制作部分,该部分在我的应用中正常工作。 当我点击indexPath.row
的一行时,我想显示另一个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
希望您能帮助我解决此问题。 谢谢
行按节索引,即每个节中的第一项具有indexPath.row == 0
。 因此,为了在展平的vc.typeOfWaste
和vc.typeOfBin
数组中查找值,您需要按照numberOfRowsInSection
方法的方式进行操作,在此方法中,您需要按字母字符过滤展平的数组,然后获取使用indexPath.row
过滤的数组的indexPath.row
。
总体而言,这种方法似乎很麻烦,必须反复过滤数据。 您的数据结构无法很好地解决要解决的问题。 我建议使用TLIndexPathTools数据模型TLIndexPathDataModel
因为它是专为表和集合视图设计的,可以将数据组织成多个部分,并可以按索引路径查找项目。 如果愿意,将很乐意指导您进行重构。
我几天前也遇到了这个问题...您需要使一个类集属性随心所欲,然后将该对象添加到一个数组中,之后您可以对一个属性进行数组排序,然后将整个数组排序
#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];
}
这是因为用于将数据传递到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数组保持排序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.