Customize UITableView in iPhone

UITableView row can be easily customized by modifying the delegate function of UITableDelegate. Listed below are some modification which can be done to get an impressive tableRow.

 

1. To add image to a UITableView.

 

-(void)tableView: (UITableView*)tableView willDisplayCell: (UITableViewCell*)cell

forRowAtIndexPath: (NSIndexPath*)indexPath {

 

UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@”tablecell.jpg”]];

[cell setBackgroundView:myImageView];

}

 

here “tablecell.jpg” is the image which gives the background image for table Row.

 

2. To Change the height of the Table Row

 

– (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

return 100.0f;

}

 

3. To determine number of sections in the Table

 

– (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

 

return count;  // Count is the number of sections you want for the table.

}

 

4. Determine the number of rows for the Table.

 

– (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

 

return [photoTitle count]; // Here photoTitle is NSArray.

}

 

5. Perform something when a row is selected. The below code is taken from one of my project. You can modify the code as per your requirement.

 

– (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

ViewSinglePhotoViewController *viewcontroller = [[ViewSinglePhotoViewController alloc] initWithNibName:@”ViewSinglePhotoViewController” bundle:nil];

NSString *passString = [LargImgUrl objectAtIndex:[indexPath row]];

NSURL *url = [NSURL URLWithString:passString];

viewcontroller.myURL = url;

viewcontroller.titleName = [photoTitle objectAtIndex:[indexPath row]];

[self.navigationController pushViewController:viewcontroller animated:YES];

[viewcontroller release];

}

 

6. Content for a row. Configuring the cell for each row.

 

– (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];

 

//configure cell here

}

return cell;

 

}

 

I explained above some of the important delegate function of UITableViewDelegate which are frequently used. For more information on other delegate function you can check Apple documentation.