如何重新加载UiViewController中的UItableView

时间:2011-09-18 03:39:24

标签: objective-c uitableview xcode4 uiviewcontroller

我在UiViewController中有一个UITableView。 在同一个屏幕中,我还有两个按钮:Show All vs Show Top 5。

现在基于全选/前5名,我必须更新表数据。

我不能使用[tableView reloadData],因为我没有使用UITableViewController。

这是我第一次使用iphone应用程序。所以任何帮助都表示赞赏。

(我使用本教程开始http://www.icodeblog.com/2009/05/24/custom-uitableviewcell-using-interface-builder/

感谢。

以下是我的代码片段:

.h文件

@interface DataViewController : UIViewController {
    NSString *showType;   
}

@property (nonatomic, retain) NSString *showType;

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

@end

.m文件

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"customCell";
    DataCustomCell *cell =  (DataCustomCell *) [tableView dequeueReusableCellWithIdentifier: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
}

1 个答案:

答案 0 :(得分:11)

像这样创建一个UITableView IBOutlet

@property (nonatomic, retain) IBOutlet UITableView *myTableView;
在你的UIViewController的接口文件中

。然后将其连接到Interface Builder中的UITableView。在您的实现文件中合成它之后,您应该能够像这样访问它

[self.myTableView reloadData];

此外,由于您保留了它,因此您必须使用dealloc方法发布myTableView。