Sergey Grishchev
Sergey Grishchev

Reputation: 12051

Create a section in UITableView like in 'Music' app

How do you create a section of UITableView with numbers for each row like numbers 1, 2, 3 for each track in 'Music' app in iOS? Thanks in advance!

enter image description here

Upvotes: 0

Views: 479

Answers (2)

skram
skram

Reputation: 5314

You can subclass UITableViewCell and create a subview UIlabel property, say..trackNumber and in your cellForRowAtIndex.. method you initialize your custom cell.

EDIT: Added example

   //CustomCell.h
@interface CustomCell : UITableViewCell

@property(nonatomic,retain) UILabel *trackNumber;

@end

//CustomCell.m
@implementation CustomCell
@synthesize trackNumber;

    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self) {
            // Initialization code
            self.trackNumber = [[[UILabel alloc] initWithFrame:CGRectMake(5,5,40,40)] autorelease];
            [self addSubview:self.trackNumber];
        }
        return self;
    }


    @end

To use it #import "CustomCell.h" and in your implementation.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";


CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {

    cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

}

cell.trackNumber.text = [NSString stringWithFormat:@"%i",[indexPath row]];

// Configure the cell.
return cell;
}

Upvotes: 2

Justin Paulson
Justin Paulson

Reputation: 4388

Create a custom UITableViewCell and do whatever you want with it. In the inspector change the cell style to Custom. Then draw in whatever you want. Make sure that you use the tag property on the view section of the new labels and other elements you create in the cell, because you will need those tags to access any custom labels, buttons, textfields, whatever that you make in the cell. Basically in your cellForRowAtIndexPath: function you can pull apart your custom cell using stuff like

UILabel *numberLabel = (UILabel *)[customCell viewWithTag:1];

Upvotes: 1

Related Questions