PDA

View Full Version : uitableview




chhoda
Nov 17, 2008, 10:58 AM
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:@"pic1.png"];
[cellViews addObject:eventsView];
[eventsView release];

CustomTableCell *newsView = [[CustomTableCell alloc] initWithFrame:frame];
newsView.image = [self getImage:@"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


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



jnic
Nov 17, 2008, 11:08 AM
You need to implement UITableViewDataSource for cellForRowAtIndexPath.

Docs: https://developer.apple.com/iphone/library/documentation/UIKit/Reference/UITableViewDataSource_Protocol/Reference/Reference.html

chhoda
Nov 18, 2008, 04:32 AM
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

chhoda
Nov 18, 2008, 07:46 AM
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

PhoneyDeveloper
Nov 18, 2008, 11:07 AM
You need to read the Table View Programming Guide. It will answer many of your questions.

jagatnibas
Nov 20, 2008, 04:35 AM
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:@"1.png"]];
[cellViews addObject:oneView];
[oneView release];

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

UITableViewCell *threeView = [[UITableViewCell alloc] initWithFrame:rect];
[threeView setImage:[self getImage:@"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];
}

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