uitableview

Discussion in 'iPhone/iPad Programming' started by chhoda, Nov 17, 2008.

  1. macrumors 6502

    #1
    Hi All,

    I am trying to do a customised UITableview by drawing different images...
    I could not really understand view controller based implementations, so i thought i would write mine. in fact i find it easier to write without using nib files

    i wrote following code

    unfortunately code never comes to cellForRowAtIndexPath:(NSIndexPath nor to drawrect of cellview ...

    what am i doing wrong ?
    ////////////////////////////////////////

    ____________________________
    inherited from TableViewCell
    _____________________________

    #import "CustomTableCell.h"

    @implementation CustomTableCell
    {
    @synthesize image;

    - (id)initWithFrame:(CGRect)frame
    {
    if (self = [super initWithFrame:frame])
    {
    // Initialization code
    }

    return self;
    }

    - (void)drawRect:(CGRect)rect
    {
    // Drawing code
    [self.image drawAtPoint:CGPointMake(0,0)];

    }

    - (void)dealloc
    {
    [super dealloc];
    }

    @end
    ___________________________________
    inherited from UITableView and implements <UITableViewDelegate>
    ___________________________________


    #import "CustomTableCell.h"
    @implementation CustomTableView
    - (UIImage*)getImage:(NSString *)imgUrl
    {
    id path = imgUrl;
    NSURL *url = [NSURL URLWithString:path];
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImage *img = [[UIImage alloc] initWithData:data];
    return img;
    }
    - (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
    // Initialization code
    cellViews = [[NSMutableArray alloc] init];
    //CGRect rect = CGRectMake(0, 0, 50, 50);
    CustomTableCell *eventsView = [[CustomTableCell alloc] initWithFrame:frame];
    eventsView.image = [self getImage:mad:"pic1.png"];
    [cellViews addObject:eventsView];
    [eventsView release];

    CustomTableCell *newsView = [[CustomTableCell alloc] initWithFrame:frame];
    newsView.image = [self getImage:mad:"pic2.png"];
    [cellViews addObject:newsView];
    [newsView release];
    }
    return self;
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    NSLog(@"row #");
    return [cellViews objectAtIndex:0];
    }

    - (void)dealloc {
    [super dealloc];
    }
    @end


    ///////////////////////////////////////
     
  2. macrumors 6502a

    #2
  3. macrumors 6502

    #3
    Thanks

    Hi jnic,

    Thanks, it worked. Now I have a couple of questions more..

    I want to put checkbox - text - image - text in each row. How would i do that ? can i create 4 columns ?

    also i tried returning more than 1 in
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

    but i did not see any difference ... what does this do ? and how does it look like with multiple sections ?

    more so i want only check box being clickable instead of the whole row.
    how can i fade the rectangle that selects the row ?

    ch
     
  4. macrumors 6502

    #4
    re: thanks

    I figured out UITableViewCellAccessoryCheckmark did the trick for me. Still the question remains, can i mark indicator only a portion of the cell ?

    regards
    ch
     
  5. macrumors 68030

    PhoneyDeveloper

    #5
    You need to read the Table View Programming Guide. It will answer many of your questions.
     
  6. macrumors regular

    #6
    going nuts over uitableview

    oh me god... i am scratching my head outa nothing over uitableview...

    I have written the following code. but when the table view is drawn, the images are coming overlapped. I dont know what is wrong with this. I have hardcoed the cell frame and changing them does not affect the cell appearance at all and middle cell overlaps a bit to top and buttom to middle.

    please give me some ideas, i am on a very strict deadline

    //////////////////////////////////////////////////////////////////

    - (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
    // Initialization code
    [self setDelegate:self];
    [self setDataSource:self];

    cellViews = [[NSMutableArray alloc] init];

    CGRect rect = CGRectMake(0, 0, 70, 170);

    UITableViewCell *oneView = [[UITableViewCell alloc] initWithFrame:rect];
    [oneView setImage:[self getImage:mad:"1.png"]];
    [cellViews addObject:eek:neView];
    [oneView release];

    UITableViewCell *twoView = [[UITableViewCell alloc] initWithFrame:rect];
    [twoView setImage:[self getImage:mad:"2.png"]];
    [cellViews addObject:twoView];
    [twoView release];

    UITableViewCell *threeView = [[UITableViewCell alloc] initWithFrame:rect];
    [threeView setImage:[self getImage:mad:"3.png"]];
    [cellViews addObject:threeView];
    [threeView release];
    }

    return self;
    }


    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    //NSLog(@"row #%d, %d", indexPath.row, indexPath.section);
    return [cellViews objectAtIndex:indexPath.row];
    }

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
    return 1;
    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    //NSLog(@"Section %d", section);
    return [cellViews count];
    }

    //////////////////////////////////////////////////////////////////
     

Share This Page