开发者

How to design buttons in table view

I hav开发者_StackOverflow中文版e 2 screens. My first screen is loading a second screen. The second screen has the subject's name in a table view. I want to add 3 buttons at the bottom of simulator ( not below the table). These 3 buttons will act as a filter. When the user clicks on the first button, whole books will be filtered by that button event. How will I add the buttons? I want to make the buttons visible to users for any number of rows in table view.

thanks


You can add UIToolbar with three UIBarButtonItems at the bottom of your view. This tutorial may help you further!


You can Use Segment controls with 3 segments and reload tableview on click of each button

code

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

    //different rows for each section and for different selected segment value
 switch (segmentedControl.selectedSegmentIndex) {
     case 0:
         return [arrayOfRestaurants count]; //counting number of restaurants
         break;
    case 1:
        return [arrayOfHotels count]; //counting number of hotels

             break;

    case 2:
         return [arrayOfPlaces count]; //counting number of famous place
             break;


 }
    return 0;

}
- (UITableViewCell *)tableView:(UITableView *)atableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    static NSString *CellIdentifier = @"Cell";


    UITableViewCell *cell = nil;



    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                       reuseIdentifier:CellIdentifier]autorelease];// cell intialization

    [cell.textLabel setFont:kFont];

    [cell.textLabel setTextColor:[UIColor colorWithRed:0 green:0 blue:80/255.0 alpha:1.0]];
    [cell.detailTextLabel setFont:[UIFont fontWithName:@"Arial" size:13.0]];

    [cell.detailTextLabel setTextColor:[UIColor whiteColor]];

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    //set up cell for different selected segments...

    cell.backgroundColor = [UIColor clearColor];
    switch (segmentedControl.selectedSegmentIndex) {
        case 0:
            NSLog(@"For Restaurants List");
            Restaurants *restaurants = [arrayOfRestaurants objectAtIndex:indexPath.row]; //getting list of restaurants

             //setting name of restaurnats to string

            cell.textLabel.text = restaurants.Name;
            cell.detailTextLabel.text = restaurants.Address; //detail textlabel to display address
            break;

        case 1:
            NSLog(@"For Hotels List");
            Hotel *hotels = [arrayOfHotels objectAtIndex:indexPath.row]; //getting list of hotels
            cell.textLabel.text = hotels.Name;
            cell.detailTextLabel.text = hotels.Address;//address of hotel
            break;

        case 2:
            NSLog(@"For Places List");
            Famousplaces *famousPlaces = [arrayOfPlaces objectAtIndex:indexPath.row];//getting list of famous places
            cell.textLabel.text = famousPlaces.Name; //name of famous place
            break;

    }   

    return cell;



}

//segment control value changed method
- (IBAction) segmentedControlIndexChanged {

    switch (segmentedControl.selectedSegmentIndex) {
        case 0:
            [tableView reloadData];//reloading table on selected segment
            break;

            case 1:
            [tableView reloadData];

                break;

            case 2:
            [tableView reloadData];
                break;

        default:
            break;
    }




}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜