开发者

UITableView abcd list indexing

开发者 https://www.devze.com 2023-04-04 14:43 出处:网络
I need to implement the abcd list indexing in the below table view right side.when i am clicking on a/b/c etc table view need to show the entries according to that for sorting like iphone contacts .ma

I need to implement the abcd list indexing in the below table view right side.when i am clicking on a/b/c etc table view need to show the entries according to that for sorting like iphone contacts .may i know what are all the changes i need for it.is need to create sections for that?please any one explain me any solution.

I have an class named as "myClass" which contains the properties iD, name and imageURL.

image url holds the photolibrary alasset url.

myClass.h

@interface myClass: NSObject {
    NSInteger iD;
    NSString *name;
    NSString *imageURL;
}

@property (nonatomic, assign) NSInteger iD;
@property (nonatomic, assign) NSString *name;
@property (nonatomic, assign) NSString *imageURL;

myClass.m

@implementation myClass

@synthesize iD;
@synthesize name;
@synthesize imageURL;

@end

So I added 50 image details with iD, name, imageURL as myClass objects in to an NSMutableArray named as *BundleImagesArray *

i displayed it in a table view. my code is:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section {
    //getting all image details with iD,name,imageURL as **myClass** objects     
    BundleImagesArray = [staticDynamicHandler getImageFromBundle];

    int count =  [BundleImagesArray count];

    for (int i = 0; i<count; i++) {
        //this array for easy scrolling after first time the table is loaded.
        [imageCollectionArrays addObject:[NSNull null]];

    }
    return count;

}

cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    //removing all the subviews
    if ([cell.contentView subviews]) {
        for (UIView *subview in [cell.contentView subviews]) {
            [subview removeFromSuperview];
        }
    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell setAccess开发者_如何学JAVAoryType:UITableViewCellAccessoryNone];

    myClass *temp = [BundleImagesArray objectAtIndex:indexPath.row];


    //adding image view 
    UIImageView *importMediaSaveImage=[[[UIImageView alloc] init] autorelease];
    importMediaSaveImage.frame=CGRectMake(0, 0, 200,135 );
    [cell.contentView addSubview:importMediaSaveImage]; 

    //adding image label
    UILabel *sceneLabel=[[[UILabel alloc] initWithFrame:CGRectMake(220,0,200,135)] autorelease]; 
    sceneLabel.font = [UIFont boldSystemFontOfSize:16.0];
    sceneLabel.textColor=[UIColor blackColor];
    [cell.contentView addSubview:sceneLabel];

    sceneLabel.text = temp.name;

    if([imageCollectionArrays objectAtIndex:indexPath.row] == [NSNull null]){ 

        //getting photlibrary image thumbnail as NSDAta
        NSData *myData = [self photolibImageThumbNailData::temp.imageURL]

        importMediaSaveImage.image =[UIImage imageWithData:myData ];

        [imageCollectionArrays replaceObjectAtIndex:indexPath.row withObject:importMediaSaveImage.image];
    } else {
        importMediaSaveImage.image = [imageCollectionArrays objectAtIndex:indexPath.row];
    }

    temp = nil;
    return cell
}

*Note:* i done it using the following way 1.Number of sections- inside this we are getting the label array and then calling the method countArrayWords whihc will count the words and the alphabets starting from a particular word. 2.Title for headerin section- Used for gettting the headers for the sections.Currently we are not using this methd as it will not look good when we are having no data on the table view. 3.number of rows in section- in this we are calling the method countArrayWords if the sectio=0. 4.cell for row at index path- this is the method in which we are calculating a variable known as h through which we are using to bind the images to the table view. However the Tableview performence is too slow with 1000 images


An easy way to handle this is to create 2 arrays, one of sections, and another of section titles. You can then populate these from your sorted array of MyClass'es in your init method (initWithObjects: , say)

- (id) initWithObjects: (NSArray *) myObjectsArray
{
    self  = [super initWithNibName: <your-nib-name> bundle: nil];

    if (self) {
        self.sections = [NSMutableArray array];
        self.sectionTitles = [NSMutableArray array];

        for (MyClass * object in self.myObjectsArray) {
            NSMutableArray * section = [self.sections lastObject];

            if (!section || ![[[[section lastObject] name] substringToIndex: 1] isEqualToString: [object.name substringToIndex: 1]]) {
                // Create a new section on change of first character

                [self.sections addObject: [NSMutableArray array]];
                [self.sectionTitles addObject: [object.name substringToIndex: 1]];
            }

            [[self.sections lastObject] addObject: object];
        }
    }

    return self;
}

Then your datasource methods are

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

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[self.sections objectAtIndex: section] count];
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Create cell

    MYClass * object = [[self.sections objectAtIndex: indexPath.section] objectAtIndex: indexPath.row];

    //Configure cell

    return cell;    
}

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

精彩评论

暂无评论...
验证码 换一张
取 消