开发者

Table view basics and appearance like MS Excel

开发者 https://www.devze.com 2023-02-12 08:44 出处:网络
I am new to Objective-C programming. Please help me understand how to insert data into a table view with a vi开发者_如何学Pythonsual style similar to MS Excel.It cannot look exactly like you want it t

I am new to Objective-C programming. Please help me understand how to insert data into a table view with a vi开发者_如何学Pythonsual style similar to MS Excel.


It cannot look exactly like you want it to, because tables in MS Excel and on the iPhone are different.

The table view on iOS has two items which you can configure to show data -- a datasoure and a delegate.

If you can fetch all the data from MS Excel in such a way that you can access any particular data just by knowing its row and column, then that data can be shown in your table view.

In a table view there is a concept of NSIndexPath, which is a structure of row numbers and corresponding section number. Now let's suppose that there is situation that your Excel document has records of one hundred students and each record has three fields: name, age, sex. In this situation you'll make a table view with one hundred sections and three rows for each section.

Now suppose you have a view controller named myViewController to show this table view, named myTableView:

-(void)viewDidLoad {

    myTableView.datasource = self; 
    myTableView.delegate = self;
}

Now before you can do that you need to do the following in the MyViewController.h file

@interface MyViewController: UIViewController <UITableViewDataSource, UITableViewDelegate> {

Now suppose you have an array named allStudents containing objects of class Student which has three properties:

NSString * name;

NSUInteger * age;

BOOL male; // yes if sex is male else NO

Now you need to implement the table view datasource and delegate methods:

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return [allStudents count];
}

-(NSInteger)tableView:(UITableView *)tableView 
numberOfRowsInSection:(NSInteger)section 
{
    return 3;
}

- (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] autorelease];
    }

    Student * current = [allStudents objectAtIndex:NSIndexPath.section];
    cell.textLabel.text = current.name;
    if( current.male ) {
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%dyr M",current.age];
    } else {
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%dyr F",current.age];
    }


    return cell;
}

You can access different properties of your table view to make it behave differently.

I hope this helped you.

0

精彩评论

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