Wednesday 6 April 2011

Custom Tableview


UITableView Custom Scrollbar


We know that UITableView inherits from UIScrollView, that means you can use any of the existing functions.
table.showsVerticalScrollIndicator = NO;

How to add Custom EditingAccessoryView for UITableView?

Create your UIButton and set to cell.editingAccessoryView        


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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
cell.editingAccessoryView = AccessoryView;//your button name
}
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}

Change iPhone UITableView delete button title while editing it 


- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
if(flag)
return UITableViewCellEditingStyleDelete;
else
return UITableViewCellEditingStyleNone;
}

UITableView sections text alignment 

cell.textLabel.textAlignment = UITextAlignRight;
 
Changing background of cell on selecting it
 
self.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"listing-gradient-hover.png"]];
self.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"listing-gradient.png"]];

Add SearchBar in UITableView

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {

[tableData removeAllObjects];

// remove all data that belongs to previous search

if([searchText isEqualToString:@""]searchText==nil){

[myTableView reloadData];

return;

}
NSInteger counter = 0;
for(NSString *name in dataSource)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
NSRange r = [name rangeOfString:searchText];
if(r.location != NSNotFound) {
if(r.location== 0)
//that is we are checking only the start of the names. {
[tableData addObject:name];

} }
counter++;
[pool release]; }
[myTableView reloadData];
}

Add Button to section

Yes it is possible.You need to use the delegate methods of UITableView and add View with all Buttons in it to TableView's header.
First set the Delegate & Datasource of UITableView then proceed with below code.
For this ,You can follow the below code:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
// create the parent view that will hold header Label
UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 300.0, 44.0)];
// create the button object
UIButton * headerBtn = [[UIButton alloc] initWithFrame:CGRectZero]; headerBtn.backgroundColor = [UIColor clearColor];
headerBtn.opaque = NO;
headerBtn.frame = CGRectMake(10.0, 0.0, 100.0, 30.0);
[headerBtn setTitle:@"" forState:UIControlEventTouchUpInside];
[headerBtn addTarget:self action:@selector(ActionEventForButton:) forControlEvents:UIControlEventTouchUpInside];
[customView addSubview:headerBtn];
return customView;
}

Once a view is added to header then set the height for header since by default it would be something 20.,so you need to adjust it according to your View HEight that has been added to header.

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 100.0; }

Creating Dynamic Cell height

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return ceil(([aText sizeWithFont:aFont].width)/self.bounds.size.width-300);
}

No comments:

Post a Comment