开发者

How to reload UItableView that is inside UiViewController

开发者 https://www.devze.com 2023-04-05 10:13 出处:网络
I have a UITableView inside UiViewController. In the same screen, I also have two buttons: Show All vs Show Top 5.

I have a UITableView inside UiViewController. In the same screen, I also have two buttons: Show All vs Show Top 5.

Now based on a selection all/top 5, I have to update table data.

I cant use [tableView reloadData] as I am not using UITableViewController.

This is the first time I am working on an iphone app. So any help is appreciated.

(I used this tutorial to get started http://www.icodeblog.com/2009/05/24/custom-uitableviewcell-using-interface-builder/)

Thanks.

Here is a snippet of my code:

.h file

@interface DataViewController : UIViewController {
    NSString *showType;   
}

@property (nonatomic, retain) NSString *showType;

-(IBAction) showTop: (id) sender;
-(IBAction) showAll: (id) sender;

@end

.m file

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"customCell";
    DataCustomCell *cell =  (DataCustomCell *) [tableView dequeueReusa开发者_JAVA技巧bleCellWithIdentifier:cellID];    

    if([showType isEqualToString:@"all"])
    {
        // use this data..
    }
    else
    {
        // use some other data..
    }    

    // ....
}

-(IBAction) showNearby: (id) sender 
{
    self.showType=@"top";  

    // reload table some way  
}

-(IBAction) showAll: (id) sender
{
    self.showType=@"all";

    //reload table some way
}


Create a UITableView IBOutlet like this

@property (nonatomic, retain) IBOutlet UITableView *myTableView;

in your UIViewController's interface file. Then have that connect to the UITableView in Interface Builder. After synthesizing it in your implementation file, you should be able to access it like this

[self.myTableView reloadData];

Also, since you retained it, you will have to release myTableView in the dealloc method.

0

精彩评论

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

关注公众号