How can i create an UITableView on Xcode 4.2,for IOS 5?

前端 未结 2 1996
-上瘾入骨i
-上瘾入骨i 2021-01-19 14:03

Last week i have downloaded Xcode 4.2, so when i started building apps i\'ve tried to add an UITableView to one of my projects (as normal as i have been doing s

2条回答
  •  孤街浪徒
    2021-01-19 14:34

    in your .h file, add the following:

    @interface YourClass: UIViewController <**UITableViewDataSource, UITableViewDelegate**>
    

    right-click (or ctrl-click) and drag from your tableView to the File's Owner twice. Once, select "delegate", and once select "dataSource".

    Then, in your .m file, you need to implement the following:

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{return 1;}
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {return someNumber;}
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
    
        [[cell textLabel] setText:yourText];
    
        return cell;
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        //do what you want to with the information at indexPath.row
    }
    

    That should get you a working tableView.

提交回复
热议问题