Xuni コントロール > FlexGrid > クイックスタート:FlexGrid へのデータの追加 |
このセクションでは、iOS アプリに FlexGrid コントロールを追加し、そこにデータを追加する方法について説明します。Objective-C で Xuni コントロールを追加する方法については、「Objective-C による Xuni コントロールの追加」を参照してください。
このトピックは 3 つの手順で構成されます。
次の図は、上記の手順を実行した後の FlexGrid を示しています。
これで、FlexGridDataSource.swift、FlexGridDataSource.h と FlexGridDataSource.m がアプリケーションに追加されます。新しいヘッダーと objective-C ファイルを追加する方法については、「Objective-C による Xuni コントロールの追加」を参照してください。追加したファイルに、次のコードを追加します。
class FlexGridDataSource: NSObject
{
var firstName, lastName: String
var address, postalCode, country: String
var customerID: Int
var city: String
var countryID: Int
var email: String
var lastOrderDate: NSDate
var orderCount: Int
var orderTotal: Double
var active: Bool
private override init()
{
self.customerID = 0
self.countryID = 0
self.firstName = ""
self.lastName = ""
self.active = false
self.address = ""
self.city = ""
self.country = ""
self.postalCode = ""
self.lastOrderDate = NSDate()
self.orderCount = 0
self.orderTotal = 0.0
self.email = ""
super.init()
}
init(customerID: Int, countryID: Int, firstName first: String, lastName last: String, address: String, city: String, country: String, postalCode: String, email: String, lastOrderDate: NSDate, orderCount: Int, orderTotal: Double, active: Bool){
self.customerID = customerID
self.countryID = countryID
self.firstName = first
self.lastName = last
self.active = active
self.address = address
self.city = city
self.country = country
self.postalCode = postalCode
self.lastOrderDate = lastOrderDate
self.orderCount = orderCount
self.orderTotal = orderTotal
self.email = email
}
class func generateRandomBool() -> Bool{
let random = (Int(arc4random_uniform(UInt32(RAND_MAX))))
if (random % 5 == 0) {
return true
}
else{
return false
}
}
class func generateRandom(max: Int) -> Int{
return (Int(arc4random_uniform(UInt32(max))))
}
class func getCustomerData(total: Int) -> NSMutableArray{
let firstNames: String = "Andy|Ben|Charlie|Dan|Ed|Fred|Gil|Herb|Jack|Karl|Larry|Mark|Noah|Oprah|Paul|Quince|Rich|Steve|Ted|Ulrich|Vic|Xavier|Zeb"
var firstNamesArray: [AnyObject] = firstNames.componentsSeparatedByString("|")
let lastNames: String = "Ambers|Bishop|Cole|Danson|Evers|Frommer|Griswold|Heath|Jammers|Krause|Lehman|Myers|Neiman|Orsted|Paulson|Quaid|Richards|Stevens|Trask|Ulam"
var lastNamesArray: [AnyObject] = lastNames.componentsSeparatedByString("|")
let countries: String = "China|India|United States|Indonesia|Brazil|Pakistan|Bangladesh|Nigeria|Russia|Japan|Mexico|Philippines|Vietnam|Germany|Ethiopia|Egypt|Iran|Turkey|Congo|France|Thailand|United Kingdom|Italy|Myanmar"
var countriesArray: [AnyObject] = countries.componentsSeparatedByString("|")
let cities: String = "Honolulu|Los Angeles|San Francisco|Las Vegas|Cancun|Chicago|New York|San Paolo|Miami|Dublin|London|Paris|Addis-Abeba|St. Petersburg|Frankfurt|Istanbul|Isfahan|Cairo|Milano|New Delhi|Bangalor|Mumbai|Dhaka|Tokyo|Osaka|Bangkok|Yangon|Manila"
var citiesArray: [AnyObject] = cities.componentsSeparatedByString("|")
let streets: String = "1st St.|Victory St.|Central Ave.|822th St."
var streetsArray: [AnyObject] = streets.componentsSeparatedByString("|")
let emails: String = "info@google.com|info@apple.com|info@microsoft.com|info@samsung.com|info@oracle.com"
var emailsArray: [AnyObject] = emails.componentsSeparatedByString("|")
let now: NSDate = NSDate()
var previousDate: NSDate
let dateComps: NSDateComponents = NSDateComponents()
let calendar: NSCalendar = NSCalendar.currentCalendar()
let array: NSMutableArray = NSMutableArray()
for var i = 0; i < total; i++ {
dateComps.day = (Int(FlexGridDataSource.generateRandom(1000)) * -1)
previousDate = calendar.dateByAddingComponents(dateComps, toDate: now, options: NSCalendarOptions(rawValue: 0))!
let customer: FlexGridDataSource = FlexGridDataSource()
customer.customerID = i
customer.countryID = FlexGridDataSource.generateRandom(countriesArray.count)
customer.country = String(countriesArray[Int(customer.countryID)])
customer.firstName = firstNamesArray[FlexGridDataSource.generateRandom(firstNamesArray.count)] as! String
customer.lastName = lastNamesArray[FlexGridDataSource.generateRandom(lastNamesArray.count)] as! String
customer.city = citiesArray[FlexGridDataSource.generateRandom(citiesArray.count)] as! String
customer.address = streetsArray[FlexGridDataSource.generateRandom(streetsArray.count)] as! String
customer.postalCode = "\(self.generateRandom(9))\(self.generateRandom(9))\(self.generateRandom(9))\(self.generateRandom(9))\(self.generateRandom(9))"
customer.lastOrderDate = previousDate
customer.email = emailsArray[FlexGridDataSource.generateRandom(emailsArray.count)] as! String
customer.orderCount = self.generateRandom(100)
customer.orderTotal = Double(self.generateRandom(100)) / 100.0 + Double(self.generateRandom(90000))
customer.active = FlexGridDataSource.generateRandomBool()
array.addObject(customer)
}
return array
}
}
?#import <Foundation/Foundation.h>
@interface FlexGridDataSource : NSObject
//プロパティを定義します。
@property NSNumber *Id;
@property NSString *Country;
@property NSNumber *Amount;
@property BOOL Active;
//データソースを定義します。
+ (NSMutableArray*)demo;
+ (NSMutableArray*)demoWithCapacity:(int)capacity;
+ (NSArray*)defaultCountries;
@end
//必要なヘッダーファイルのインポート。
#import "FlexGridDataSource.h"
static NSMutableArray *a;
@implementation FlexGridDataSource
+ (NSMutableArray*)demo {
return [FlexGridDataSource demoWithCapacity:10];
}
+ (NSMutableArray*)demoWithCapacity:(int)capacity
{
if (a == nil || a.count < capacity)
{
a = [[NSMutableArray alloc] initWithCapacity:capacity];
srand48(time(0));
for (int i = 0; i < capacity; i++) {
FlexGridDataSource *s = [[FlexGridDataSource alloc] init];
s.Id = [NSNumber numberWithInt:i];
s.Country = [FlexGridDataSource defaultCountries][i % [FlexGridDataSource defaultCountries].count];
s.Amount = [NSNumber numberWithDouble:drand48()*1000];
s.Active = (i % 4) == 0;
[a addObject:s];
}
}
if (capacity < a.count) {
return [NSMutableArray arrayWithArray:[a subarrayWithRange:NSMakeRange(0, capacity)]];
}
return a;
}
+ (NSArray*)defaultCountries {
return @[@"Germany", @"Greece", @"Italy", @"Japan", @"UK", @"US"];
}
@end
using System;
using System.Collections.Generic;
namespace Xuni_QuickStart
{
public class FlexGridDataSource
{
static string[] _countries = "China|India|United States|Indonesia|Brazil|Pakistan|Bangladesh|Nigeria|Russia|Japan|Mexico|Philippines|Vietnam|Germany|
Ethiopia|Egypt|Iran|Turkey|Congo|France|Thailand|United Kingdom|Italy|Myanmar".Split('|');
public List<demodata> getData(int count)
{
// ランダムデータの作成
var _rnd = new Random();
var _data = new List<demodata>();
for (int i = 0; i < count; i++)
{
var ddata = new demodata();
ddata.ID = i+1;
ddata.Country = _countries[_rnd.Next(_countries.Length - 1)];
ddata.Amount = _rnd.Next(count * 100);
ddata.Active = (_rnd.Next(count) % 2) == 0;
_data.Add(ddata);
}
return _data;
}
public FlexGridDataSource()
{
}
}
public class demodata
{
private int _id;
private string _country;
private long _amount;
private bool _active;
public int ID { get { return _id; } set { _id = value; } }
public string Country { get { return _country; } set { _country = value; } }
public long Amount { get { return _amount; } set { _amount = value; } }
public bool Active { get { return _active; } set { _active = value; } }
}
}
FlexGrid コントロールを初期化するには、次の手順を実行します。
MainStoryboard
をクリックしてストーリーボードエディタを開きます。FlexGrid コントロールを初期化するには、Project Navigator から ViewController.m
またはViewController.swift
ファイルを開き、その内容を次のコードで置き換えます。これは、FlexGrid を初期化するために、View コントローラの viewDidLoad メソッドをオーバーライドします。
//必要なヘッダーファイルのインポート。
import UIKit
import XuniFlexGridKit
class ViewController: UIViewController
{
var grid = FlexGrid()
override func viewDidLoad()
{
super.viewDidLoad()
// ビューを読み込んだ後の追加セットアップを行います(通常は nib から)。
grid.isReadOnly = true
grid.itemsSource = FlexGridDataSource.getCustomerData(10)
grid.autoSizeColumns();
self.view.addSubview(grid)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
grid.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height/3)
}
}
//必要なヘッダーファイルのインポート。?
#import "ViewController.h"
#import <XuniFlexGridKit/XuniFlexGridKit.h>
#import <XuniCoreKit/XuniCoreKit.h>
#import "FlexGridDataSource.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// ビューを読み込んだ後の追加セットアップを行います(通常は nib から)。
//FlexGridコントロールを初期化します。
FlexGrid *grid = [[FlexGrid alloc] init];
//FlexGridをデータにバインドします。
grid.itemsSource = [FlexGridDataSource demo];
grid.autoGenerateColumns = true;
grid.allowSorting = true;
self.view = grid;
}
@end
using System;
using UIKit;
namespace Xuni_QuickStart
{
public partial class ViewController : UIViewController
{
protected ViewController(IntPtr handle) : base(handle)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
//ライセンスの設定
Xuni.iOS.Core.XuniLicenseManager.Key = License.Key;
flexgrid.ItemsSource = new FlexGridDataSource().getData(100);
flexgrid.AutoGenerateColumns = true;
flexgrid.AllowSorting = true;
}
public override void DidReceiveMemoryWarning()
{
base.DidReceiveMemoryWarning();
}
}
}
Command-R を押してアプリケーションを実行します。