tags:

views:

317

answers:

2

hi , ihave application that is viewbased and i am adding tableview as subview to main view

and i have taken UITableViewDelegate to respond the table methods

everything is working fine

but i want to select first row or UITableView as default selected(Highlighted) plz help me if possible then send me sample code and where i have to place that code means in which method or event

Thanks In Advance

A: 

We use custom background images for the cell based on whether or not it is the first cell... a middle cell or the last cell. That way we get a nice rounded corner look to the whole table. When the row is selected, it swaps out a nice 'highlighted' cell to give the user feed back that they have selected a cell.

UIImage *rowBackground;
UIImage *selectionBackground;
NSInteger sectionRows = [tableView numberOfRowsInSection:[indexPath section]];
NSInteger row = [indexPath row];

if (row == 0 && row == sectionRows - 1)
{
    rowBackground = [UIImage imageNamed:@"topAndBottomRow.png"];
    selectionBackground = [UIImage imageNamed:@"topAndBottomRowSelected.png"];
}
else if (row == 0)
{
    rowBackground = [UIImage imageNamed:@"topRow.png"];
    selectionBackground = [UIImage imageNamed:@"topRowSelected.png"];
}
else if (row == sectionRows - 1)
{
    rowBackground = [UIImage imageNamed:@"bottomRow.png"];
    selectionBackground = [UIImage imageNamed:@"bottomRowSelected.png"];
}
else
{
    rowBackground = [UIImage imageNamed:@"middleRow.png"];
    selectionBackground = [UIImage imageNamed:@"middleRowSelected.png"];
}


((UIImageView *)cell.backgroundView).image = rowBackground;
((UIImageView *)cell.selectedBackgroundView).image = selectionBackground;

If you wish just make the first cell, that which is at indexPath.row == 0, to use a custom background.

This is derived from Matt Gallagher's excellent site

Michael
A: 

You can do like this:

- (void)viewDidLoad {
    [super viewDidLoad];
    NSIndexPath *ip=[NSIndexPath indexPathForRow:0 inSection:0];
    [myTableView selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionBottom];
}
dombesz