Add an index to the table view of IOS and an index to the ios View
The effect we want to achieve is as follows.
# Import <UIKit/UIKit. h>
@ Interface IkrboyViewController5: UIViewController {
NSMutableDictionary * dict;
}
@ End
#import <UIKit/UIKit.h>@interface IkrboyViewController5 : UIViewController{ NSMutableDictionary *dict;}@end
2. Add the following changes in ControlView. m:
Cpp Code
-(Void) viewDidLoad
{
[Super viewDidLoad];
[Self initTableViewData];
// Do any additional setup after loading the view.
}
-(Void) initTableViewData {
NSBundle * bundle = [NSBundle mainBundle];
NSString * plistPath = [bundle pathForResource: @ "user_head" ofType: @ "plist"];
NSArray * dataArr = [[NSArray alloc] initWithContentsOfFile: plistPath];
// Divide all data into three groups
NSMutableArray * arr1 = [NSMutableArray array];
NSMutableArray * arr2 = [NSMutableArray array];
NSMutableArray * arr3 = [NSMutableArray array];
Dict = [NSMutableDictionary dictionary];
[Dict setObject: arr1 forKey: @ "Group1"];
[Dict setObject: arr2 forKey: @ "Group2"];
[Dict setObject: arr3 forKey: @ "Group3"];
// Set the basis for data division, that is, the data is divided into three groups based on the index, the first group is the index 0-1, the second group is 2-4, and the fifth group is the third group.
For (NSInteger index = 0; index <[dataArr count]; index ++ ){
NSDictionary * item = [dataArr objectAtIndex: index];
If (index <2 ){
[Arr1 addObject: item];
}
Else if (index> = 2 & index <5 ){
[Arr2 addObject: item];
}
Else if (index> = 5 ){
[Arr3 addObject: item];
}
}
}
-(Void) viewDidLoad {[super viewDidLoad]; [self initTableViewData]; // Do any additional setup after loading the view .} -(void) initTableViewData {NSBundle * bundle = [NSBundle mainBundle]; NSString * plistPath = [bundle pathForResource: @ "user_head" ofType: @ "plist"]; NSArray * dataArr = [[NSArray alloc] routing: plistPath]; // divide all data into three groups of partitions * arr1 = [NSMutableArray array]; NSMutableArray * arr2 = [NSMutableArray array]; NSMutableArray * arr3 = [NSMutableArray array]; dict = [NSMutableDictionary dictionary]; [dict setObject: arr1 forKey: @ "Group1"]; [dict setObject: arr2 forKey: @ "Group2"]; [dict setObject: arr3 forKey: @ "Group3"]; // you can specify the basis for Data Division by index, if the index is 0-1, the first group is displayed. If the index is 2-4, the second group is displayed. If the index is 5, the third group is for (NSInteger index = 0; index <[dataArr count]; index ++) {NSDictionary * item = [dataArr objectAtIndex: index]; if (index <2) {[arr1 addObject: item];} else if (index> = 2 & index <5) {[arr2 addObject: item];} else if (index >=5) {[arr3 addObject: item] ;}}
3. initialize TableView
Cpp Code
// Number of groups
-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView
{
Return [[dict allKeys] count];
}
// Number of data units for each group
-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section
{
Switch (section)
{
Case 0:
{
Return [[dict objectForKey: @ "Group1"] count];
}
Case 1:
{
Return [[dict objectForKey: @ "Group2"] count];
}
Case 2:
{
Return [[dict objectForKey: @ "Group3"] count];
}
}
Return 0;
}
// The group title. The following method is not implemented and the group title is not displayed.
-(NSString *) tableView :( UITableView *) tableView titleForHeaderInSection :( NSInteger) section
{
// The keys arr retrieved by dict allKeys are unordered and need to be sorted.
NSArray * arr = [[dict allKeys] sortedArrayUsingSelector: @ selector (compare :)];
Return [arr objectAtIndex: section];
}
// Index prompt on the right of the list. The following method is not implemented and the right index is not displayed.
-(NSArray *) sectionIndexTitlesForTableView: (UITableView *) tableView
{
// The keys arr retrieved by dict allKeys are unordered and need to be sorted.
NSArray * arr = [[dict allKeys] sortedArrayUsingSelector: @ selector (compare :)];
Return arr;
}
-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath
{
Static NSString * CellIdentifier = @ "myTableCell ";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
NSUInteger row = [indexPath row];
NSUInteger section = [indexPath section];
NSArray * arr;
Switch (section ){
Case 0:
Arr = [dict objectForKey: @ "Group1"];
Break;
Case 1:
Arr = [dict objectForKey: @ "Group2"];
Break;
Case 2:
Arr = [dict objectForKey: @ "Group3"];
Break;
Default:
Break;
}
NSDictionary * rowDict = [arr objectAtIndex: row];
Cell. textLabel. text = [rowDict objectForKey: @ "itemName"];
NSLog (@ "cell. label. text = % @", [rowDict objectForKey: @ "itemName"]);
NSString * imagePath = [rowDict objectForKey: @ "itemImagePath"];
Cell. imageView. image = [UIImage imageNamed: imagePath];
NSLog (@ "cell. image. image = % @", imagePath );
Cell. accessoryType = UITableViewCellAccessoryDisclosureIndicator;
Return cell;
}
// Select the Cell to respond to the event
-(Void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {
[TableView deselectRowAtIndexPath: indexPath animated: YES]; // The selected reversed color disappears immediately.
NSUInteger row = [indexPath row];
NSUInteger section = [indexPath section];
NSArray * arr;
Switch (section ){
Case 0:
Arr = [dict objectForKey: @ "Group1"];
Break;
Case 1:
Arr = [dict objectForKey: @ "Group2"];
Break;
Case 2:
Arr = [dict objectForKey: @ "Group3"];
Break;
Default:
Break;
}
NSDictionary * rowDict = [arr objectAtIndex: row];
NSString * userName = [rowDict objectForKey: @ "itemName"];
NSLog (@ "userName = % @", userName );
}
// Number of groups-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {return [[dict allKeys] count];} // number of data units in each group-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {switch (section) {case 0: {return [[dict objectForKey: @ "Group1"] count];} case 1: {return [[dict objectForKey: @ "Group2"] count];} case 2: {return [[dict objectForKey: @ "Group3"] count] ;}} return 0 ;} // The group title. The following method is not implemented, and the group title is not displayed-(NSString *) tableView :( UITableView *) tableView titleForHeaderInSection :( NSInteger) section {// The key arr retrieved by dict allKeys is unordered and needs to be sorted. NSArray * arr = [[dict allKeys] sortedArrayUsingSelector: @ selector (compare :)]; return [arr objectAtIndex: section];} // index prompt on the right of the list. The following method is not implemented, and the right index-(NSArray *) sectionIndexTitlesForTableView: (UITableView *) is not displayed *) tableView {// The key arr retrieved by dict allKeys is unordered and needs to be sorted. NSArray * arr = [[dict allKeys] sortedArrayUsingSelector: @ selector (compare :)]; return arr ;} -(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {static NSString * CellIdentifier = @ "myTableCell"; UITableViewCell * cell = [tableView progress: progress]; NSUInteger row = [indexPath row]; NSUInteger section = [indexPath section]; NSArray * arr; switch (section) {case 0: arr = [dict objectForKey: @ "Group1"]; break; case 1: arr = [dict objectForKey: @ "Group2"]; break; case 2: arr = [dict objectForKey: @ "Group3"]; break; default: break;} NSDictionary * rowDict = [arr objectAtIndex: row]; cell. textLabel. text = [rowDict objectForKey: @ "itemName"]; NSLog (@ "cell. label. text = % @ ", [rowDict objectForKey: @" itemName "]); NSString * imagePath = [rowDict objectForKey: @" itemImagePath "]; cell. imageView. image = [UIImage imageNamed: imagePath]; NSLog (@ "cell. image. image = % @ ", imagePath); cell. accessoryType = response; return cell;} // select the Cell response event-(void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {[tableView deselectRowAtIndexPath: indexPath animated: YES]; // The reversed color after selection instantly disappears NSUInteger row = [indexPath row]; NSUInteger section = [indexPath section]; NSArray * arr; switch (section) {case 0: arr = [dict objectForKey: @ "Group1"]; break; case 1: arr = [dict objectForKey: @ "Group2"]; break; case 2: arr = [dict objectForKey: @ "Group3"]; break; default: break;} NSDictionary * rowDict = [arr objectAtIndex: row]; NSString * userName = [rowDict objectForKey: @ "itemName"]; NSLog (@ "userName = % @", userName );}
- UITableView.zip (22.2 KB)