Complete demo of IOS similar to iphone Address Book TableView [with source code]

Source: Internet
Author: User

Objective-c: a self-written demo. If you have any shortcomings, please correct them:
:




Core code:
[Plain]
-(Void) viewDidLoad
{
[Super viewDidLoad];

NSArray * array = [[NSArray alloc] initWithObjects: @ "hello", @ "BFlower ",
@ "CGrass", @ "DFence", @ "EHouse", @ "FTable", @ "GChair ",
@ "HBook", @ "ISwing", @ "JWang", @ "KDong", @ "LNi", @ "MHao", @ "Na", @ "Oa ", @ "Pa", @ "Qa", @ "Ra", @ "Sa", @ "Ta", @ "Ua", @ "Va", @ "Wa ", @ "Xa", @ "Ya", @ "Za", nil];
Self. listarray = array;
NSLog (@ "listarryCount: % d", [listarray count]);
SecLabelArray = [[NSArray alloc] initWithObjects: @ "A", @ "B", @ "C", @ "D", @ "E", @ "F ", @ "G", @ "H", @ "I", @ "J", @ "K", @ "L", @ "M", @ "N ", @ "O", @ "P", @ "Q", @ "R", @ "S", @ "T", @ "U", @ "V ", @ "W", @ "X", @ "Y", @ "Z", nil];

NSArray * arrayA = [[NSArray alloc] initWithObjects: @ "test A1", @ "test A2", nil];
NSArray * arrayB = [[NSArray alloc] initWithObjects: @ "test B1", @ "test B2", @ "test B3", nil];
NSArray * arrayC = [[NSArray alloc] initWithObjects: @ "test C1", @ "test C2", @ "test C3", @ "test C4", nil];
NSArray * arrayD = [[NSArray alloc] initWithObjects: @ "test D1", @ "test D2", @ "test D3", @ "test D4 ", @ "test D5", nil];
NSArray * arrayE = [[NSArray alloc] initWithObjects: @ "test E1", @ "test E2", @ "test E3", @ "test E4 ", @ "test E5", @ "test E6", nil];
NSArray * arrayF = [[NSArray alloc] initWithObjects: @ "test F1", @ "test F2", @ "test F3", @ "test F4 ", @ "test F5", @ "test F6", @ "test F7", nil];
NSArray * arrayG = [[NSArray alloc] initWithObjects: @ "test G1", @ "test G2", @ "test G3", @ "test G4 ", @ "test G5", @ "test G6", nil];
ArrayDictKey = [[NSArray alloc] initWithObjects: @ "A", @ "B", @ "C", @ "D", @ "E", @ "F ", @ "G", nil];
ArrayDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys: arrayA, [arrayDictKey objectAtIndex: 0],
ArrayB, [arrayDictKey objectAtIndex: 1],
ArrayC, [arrayDictKey objectAtIndex: 2],
ArrayD, [arrayDictKey objectAtIndex: 3],
ArrayE, [arrayDictKey objectAtIndex: 4],
ArrayF, [arrayDictKey objectAtIndex: 5],
ArrayG, [arrayDictKey objectAtIndex: 6],
Nil];
NSLog (@ "arrayrow: % d", [[arrayDict objectForKey: [arrayDictKey objectAtIndex: 1] count]);

TableView = [[UITableView alloc] initWithFrame: CGRectMake (0, 0,320,460)];
[TableView setDelegate: self];
[TableView setDataSource: self];
[Self. view addSubview: tableView];
[TableView release];
 

// Do any additional setup after loading the view.
}
 
-(NSInteger) numberOfSectionsInTableView :( UITableView *) aTableView
{
// * Several groups appear
// If (aTableView = self. tableView) return 27;
Return [arrayDict count];
}
 
// * Alphabetic search
-(NSArray *) sectionIndexTitlesForTableView :( UITableView *) tableView
{
// * Letter index list
/* NSMutableArray * toBeReturned = [[NSMutableArray alloc] init];

For (char c = 'a'; c <= 'Z'; c ++)

[ToBeReturned addObject: [NSString stringWithFormat: @ "% c", c]; */

Return arrayDictKey;

/* NSMutableArray * newarr = [[NSMutableArray alloc] initWithArray: listarray];
[Newarr addObject: @ "{search}"]; // equivalent to [arr addObject: UITableViewIndexSearch];
Return newarr ;*/

}
 
-(NSInteger) tableView :( UITableView *) tableView sectionForSectionIndexTitle :( NSString *) title atIndex :( NSInteger) index
{
// The number of indexed groups displayed during the search
NSInteger count = 0;
NSLog (@ "% @", title );
For (NSString * character in arrayDictKey)
{

If ([character Ister tostring: title])
{

Return count;

}

Count ++;

}

Return count;

}
 
-(NSString *) tableView :( UITableView *) tableView titleForHeaderInSection :( NSInteger) section
{

/* If ([listarray count] = 0)

{

Return @"";

}*/

// Return [listarray objectAtIndex: section]; // * Group tag
Return [arrayDictKey objectAtIndex: section];

}
 
-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {
// Return [self. listarray count]; // * number of lines to be displayed in each group
// NSInteger I = [[listarray objectAtIndex: section]
NSInteger I = [[arrayDict objectForKey: [arrayDictKey objectAtIndex: section] count];
Return I;
}
 
-(UITableViewCellAccessoryType) tableView :( UITableView *) tableView accessoryTypeForRowWithIndexPath :( NSIndexPath *) indexPath {
// Return type selection button
 
Return UITableViewCellAccessoryDisclosureIndicator; // The icon on the right of each row
}
-(UITableViewCell *) tableView :( UITableView *) tableview
CellForRowAtIndexPath :( NSIndexPath *) indexPath {

Static NSString * TableSampleIdentifier = @ "TableSampleIdentifier ";

UITableViewCell * cell = [tableview dequeueReusableCellWithIdentifier:
TableSampleIdentifier];
If (cell = nil ){
Cell = [[UITableViewCell alloc]
InitWithStyle: UITableViewCellStyleDefault
ReuseIdentifier: TableSampleIdentifier];
}

NSUInteger row = [indexPath row];
NSUInteger sectionMy = [indexPath section];
NSLog (@ "sectionMy: % d", sectionMy );
 
Cell. textLabel. text = [[arrayDict objectForKey: [arrayDictKey objectAtIndex: sectionMy] objectAtIndex: row]; // The text displayed for each row

NSString * str = [NSString stringWithFormat: @ "% d", row];

UIImage * image = [UIImage imageNamed: str];
Cell. imageView. image = image;
UIImage * highLighedImage = [UIImage imageNamed: @ "1.png"];

Cell. imageView. highlightedImage = highLighedImage; // Changes the header image when a row is selected
Return cell;
}
// Indicates whether the del button is displayed on the cell.
-(BOOL) tableView :( UITableView *) tableView canEditRowAtIndexPath :( NSIndexPath *) indexPath {


Return YES; // whether to delete the icon
}
// Edit the status (do not know what to do)
-(Void) tableView :( UITableView *) tableView commitEditingStyle :( UITableViewCellEditingStyle) editingStyle
ForRowAtIndexPath :( NSIndexPath *) indexPath
{

[Self viewDidLoad];
}
 
// The operation that is performed when selected
-(NSIndexPath *) tableView :( UITableView *) tableView
WillSelectRowAtIndexPath :( NSIndexPath *) indexPath {
NSUInteger row = [indexPath row];
If (row % 2 = 0 ){
NSUInteger row = [indexPath row];
NSString * rowValue = [listarray objectAtIndex: row];

NSString * message = [[NSString alloc] initWithFormat:
@ "You selected % @", rowValue];
UIAlertView * alert = [[UIAlertView alloc]
InitWithTitle: @ "Row Selected! "
Message: message
Delegate: nil
CancelButtonTitle: @ "Yes I Did"
OtherButtonTitles: nil];
[Alert show];

}
Return indexPath;
}
-(CGFloat) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath {

// Control the value size here
Return 50.0; // control Row Height

}
Conclusion: Some things are difficult and complicated before they are useless. After they are done, we find that the key lies in understanding and more practices... look at my demo and study it...
Demo: http://www.bkjia.com/uploadfile/2012/0525/20120525022534806.zip
 

 

 

From the column dongge_111

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.