精品久久影视,日韩欧美精品一区二区综合视频,青青在线精品http://www.aygfsteel.com/chenying19890808/category/54498.html<span style="color: red"><span style="font-family: 幼圓"><span style="color: red; font-family: ">自己選擇的路,摸爬滾打也要走下去</span> </span></span>
zh-cnWed, 20 Aug 2014 02:55:44 GMTWed, 20 Aug 2014 02:55:44 GMT60- IOS開發筆記 多個tableView的使用http://www.aygfsteel.com/chenying19890808/archive/2014/08/20/417150.htmlwokaoJunewokaoJuneWed, 20 Aug 2014 02:44:00 GMThttp://www.aygfsteel.com/chenying19890808/archive/2014/08/20/417150.htmlhttp://www.aygfsteel.com/chenying19890808/comments/417150.htmlhttp://www.aygfsteel.com/chenying19890808/archive/2014/08/20/417150.html#Feedback0http://www.aygfsteel.com/chenying19890808/comments/commentRss/417150.htmlhttp://www.aygfsteel.com/chenying19890808/services/trackbacks/417150.htmlUITableView是app開發中常用到的控件,功能很強大,多用于數據的顯示。下面以一個簡單的實例來介紹tableview的基本用法。(適合新手,高手飄過)

- @interface TableViewTestViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{
-
- UITableView *DataTable;
-
- NSMutableArray *dataArray1; //定義數據數組1
-
- NSMutableArray *dataArray2;//定義數據數組2
-
- NSMutableArray *titleArray;//定義標題數組
-
- }
-
-
-
- - (void)viewDidLoad
-
- {
-
- [superviewDidLoad];
-
- //初始化tableview
-
- DataTable = [[UITableViewalloc] initWithFrame:CGRectMake(0, 0, 320, 420)];//指定位置大小
-
- [DataTablesetDelegate:self];//指定委托
-
- [DataTablesetDataSource:self];//指定數據委托
-
- [self.viewaddSubview:DataTable];//加載tableview
-
-
-
- dataArray1 = [[NSMutableArrayalloc] initWithObjects:@"中國", @"美國", @"英國", nil];//初始化數據數組1
-
- dataArray2 = [[NSMutableArrayalloc] initWithObjects:@"黃種人", @"黑種人", @"白種人", nil];//初始化數據數組2
-
- titleArray = [[NSMutableArrayalloc] initWithObjects:@"國家", @"種族", nil];//初始化標題數組
-
-
-
- }
-
-
-
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
-
- {
-
- // Return YES for supported orientations
-
- return (interfaceOrientation == UIInterfaceOrientationPortrait);
-
- }
-
-
-
-
-
- //每個section顯示的標題
-
- - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
-
- switch (section) {
-
- case 0:
-
- return [titleArray objectAtIndex:section];//提取標題數組的元素用來顯示標題
-
- case 1:
-
- return [titleArray objectAtIndex:section];//提取標題數組的元素用來顯示標題
-
- default:
-
- return @"Unknown";
-
- }
-
-
-
- }
-
-
-
- //指定有多少個分區(Section),默認為1
-
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
-
- return [titleArray count];//返回標題數組中元素的個數來確定分區的個數
-
- }
-
-
-
- //指定每個分區中有多少行,默認為1
-
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
-
- switch (section) {
-
- case 0:
-
- return [dataArray1 count];//每個分區通常對應不同的數組,返回其元素個數來確定分區的行數
-
- break;
-
- case 1:
-
- return [dataArray2 count];
-
- break;
-
- default:
-
- return 0;
-
- break;
-
- }
-
- }
-
-
-
- //繪制Cell
-
- -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
-
-
-
- static NSString *CellIdentifier = @"Cell";
-
- //初始化cell并指定其類型,也可自定義cell
-
- UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
-
- if(cell == nil)
-
- {
-
- cell = [[[UITableViewCellalloc]
-
- initWithFrame:CGRectZero
-
- reuseIdentifier:CellIdentifier] autorelease];
-
- }
-
- switch (indexPath.section) {
-
- case 0://對應各自的分區
-
- [[cell textLabel] setText:[dataArray1 objectAtIndex:indexPath.row]];//給cell添加數據
-
- break;
-
- case 1:
-
- [[cell textLabel] setText:[dataArray2 objectAtIndex:indexPath.row]];
-
- break;
-
- default:
-
- [[cell textLabel] setText:@"Unknown"];
-
- }
-
- return cell;//返回cell
-
- }
上面的例子在功能上介紹了tableview的使用,但其在數據處理上具有很大的局限性。當我們要從服務器上請求數據,面對多種可能的數據(主要指數組的個數不穩定)此時上面的switch將無法滿足我們的需求了。使用switch可是代碼的結構清晰明了,但其局限性很致命(switch中case的個數無法動態指定),下面的另一種方法可解決上述問題。

代碼在原由基礎上進行的修改:
- @interface TableViewTestViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{
-
- UITableView *DataTable;
-
- NSMutableArray *dataArray1;
-
- NSMutableArray *dataArray2;
-
- NSMutableArray *titleArray;
-
- NSMutableArray *dataArray; //加入了用于保存數組的數組 dataArray
-
- }
-
-
-
- - (void)viewDidLoad
-
- {
-
- [superviewDidLoad];
-
- DataTable = [[UITableViewalloc] initWithFrame:CGRectMake(0, 0, 320, 420)];
-
- [DataTablesetDelegate:self];
-
- [DataTablesetDataSource:self];
-
- [self.viewaddSubview:DataTable];
-
-
-
- dataArray1 = [[NSMutableArrayalloc] initWithObjects:@"中國", @"美國", @"英國", nil];
-
- dataArray2 = [[NSMutableArrayalloc] initWithObjects:@"黃種人", @"黑種人", @"白種人", nil];
-
- titleArray = [[NSMutableArrayalloc] initWithObjects:@"國家", @"種族", nil];
-
- dataArray = [[NSMutableArrayalloc] initWithObjects:dataArray1, dataArray2, nil]; //初始化dataArray,元素為數組
-
-
-
- }
-
-
-
-
-
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
-
- {
-
- // Return YES for supported orientations
-
- return (interfaceOrientation == UIInterfaceOrientationPortrait);
-
- }
-
- //制定個性標題,這里通過UIview來設計標題,功能上豐富,變化多。
-
- - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
-
- UIView *view = [[[UIViewalloc] initWithFrame:CGRectMake(0, 0, 320, 40)] autorelease];
-
- [view setBackgroundColor:[UIColorbrownColor]];//改變標題的顏色,也可用圖片
-
- UILabel *label = [[UILabelalloc] initWithFrame:CGRectMake(5, 5, 100, 30)];
-
- label.textColor = [UIColorredColor];
-
- label.backgroundColor = [UIColorclearColor];
-
- label.text = [titleArrayobjectAtIndex:section];
-
- [view addSubview:label];
-
- return view;
-
- }
-
- //指定標題的高度
-
- - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
-
- return 40;
-
- }
-
-
-
- //每個section顯示的標題,有了上面的這個就不要了
-
- - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
-
- }
-
-
-
- //指定有多少個分區(Section),默認為1
-
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
-
- return [titleArraycount];
-
- }
-
-
-
- //指定每個分區中有多少行,默認為1
-
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
-
- /* switch (section) {
-
- case 0:
-
- return [dataArray1 count];
-
- break;
-
- case 1:
-
- return [dataArray2 count];
-
- break;
-
- default:
-
- return 0;
-
- break;
-
- }*/
-
- /* for(int i = 0; i < [titleArray count]; i++){
-
- if(section == i){
-
- return [[dataArray objectAtIndex:section] count];
-
- }
-
- }*/
-
- //上面的方法也是可行的,大家參考比較下
-
- return [[dataArray objectAtIndex:section] count]; //取dataArray中的元素,并根據每個元素(數組)來判斷分區中的行數。
-
-
-
-
-
- }
-
-
-
- //繪制Cell
-
- -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
-
-
-
- static NSString *CellIdentifier = @"Cell";
-
-
-
- UITableViewCell *cell = (UITableViewCell*)[tableView
-
- dequeueReusableCellWithIdentifier:CellIdentifier];
-
- if(cell == nil)
-
- {
-
- cell = [[[UITableViewCellalloc]
-
- initWithFrame:CGRectZero
-
- reuseIdentifier:CellIdentifier] autorelease];
-
- }
-
-
-
- /*switch (indexPath.section) {
-
- case 0:
-
- [[cell textLabel]
-
- setText:[dataArray1 objectAtIndex:indexPath.row]];
-
- break;
-
- case 1:
-
- [[cell textLabel]
-
- setText:[dataArray2 objectAtIndex:indexPath.row]];
-
- break;
-
- default:
-
- [[cell textLabel]
-
- setText:@"Unknown"];
-
- }*/
-
- //上面的方法也可行,大家比較下。
-
- [[cell textLabel] setText:[[dataArrayobjectAtIndex:indexPath.section]objectAtIndex:indexPath.row]];
-
- //同上,取出dataArray中每個分區所對應的元素(數組),并通過其來取值。 (大家要有想像力, 復制代碼試試就明白了)
-
-
-
- return cell;
-
-
-
- }
-
-
-
- //改變行的高度
-
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
-
- return40;
-
- }
轉自:http://www.cnblogs.com/top5/archive/2012/05/17/2506604.html

]]> - IOS開發筆記 多個tableView的使用http://www.aygfsteel.com/chenying19890808/archive/2014/08/20/417151.htmlwokaoJunewokaoJuneWed, 20 Aug 2014 02:44:00 GMThttp://www.aygfsteel.com/chenying19890808/archive/2014/08/20/417151.htmlhttp://www.aygfsteel.com/chenying19890808/comments/417151.htmlhttp://www.aygfsteel.com/chenying19890808/archive/2014/08/20/417151.html#Feedback0http://www.aygfsteel.com/chenying19890808/comments/commentRss/417151.htmlhttp://www.aygfsteel.com/chenying19890808/services/trackbacks/417151.htmlUITableView是app開發中常用到的控件,功能很強大,多用于數據的顯示。下面以一個簡單的實例來介紹tableview的基本用法。(適合新手,高手飄過)

- @interface TableViewTestViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{
-
- UITableView *DataTable;
-
- NSMutableArray *dataArray1; //定義數據數組1
-
- NSMutableArray *dataArray2;//定義數據數組2
-
- NSMutableArray *titleArray;//定義標題數組
-
- }
-
-
-
- - (void)viewDidLoad
-
- {
-
- [superviewDidLoad];
-
- //初始化tableview
-
- DataTable = [[UITableViewalloc] initWithFrame:CGRectMake(0, 0, 320, 420)];//指定位置大小
-
- [DataTablesetDelegate:self];//指定委托
-
- [DataTablesetDataSource:self];//指定數據委托
-
- [self.viewaddSubview:DataTable];//加載tableview
-
-
-
- dataArray1 = [[NSMutableArrayalloc] initWithObjects:@"中國", @"美國", @"英國", nil];//初始化數據數組1
-
- dataArray2 = [[NSMutableArrayalloc] initWithObjects:@"黃種人", @"黑種人", @"白種人", nil];//初始化數據數組2
-
- titleArray = [[NSMutableArrayalloc] initWithObjects:@"國家", @"種族", nil];//初始化標題數組
-
-
-
- }
-
-
-
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
-
- {
-
- // Return YES for supported orientations
-
- return (interfaceOrientation == UIInterfaceOrientationPortrait);
-
- }
-
-
-
-
-
- //每個section顯示的標題
-
- - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
-
- switch (section) {
-
- case 0:
-
- return [titleArray objectAtIndex:section];//提取標題數組的元素用來顯示標題
-
- case 1:
-
- return [titleArray objectAtIndex:section];//提取標題數組的元素用來顯示標題
-
- default:
-
- return @"Unknown";
-
- }
-
-
-
- }
-
-
-
- //指定有多少個分區(Section),默認為1
-
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
-
- return [titleArray count];//返回標題數組中元素的個數來確定分區的個數
-
- }
-
-
-
- //指定每個分區中有多少行,默認為1
-
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
-
- switch (section) {
-
- case 0:
-
- return [dataArray1 count];//每個分區通常對應不同的數組,返回其元素個數來確定分區的行數
-
- break;
-
- case 1:
-
- return [dataArray2 count];
-
- break;
-
- default:
-
- return 0;
-
- break;
-
- }
-
- }
-
-
-
- //繪制Cell
-
- -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
-
-
-
- static NSString *CellIdentifier = @"Cell";
-
- //初始化cell并指定其類型,也可自定義cell
-
- UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
-
- if(cell == nil)
-
- {
-
- cell = [[[UITableViewCellalloc]
-
- initWithFrame:CGRectZero
-
- reuseIdentifier:CellIdentifier] autorelease];
-
- }
-
- switch (indexPath.section) {
-
- case 0://對應各自的分區
-
- [[cell textLabel] setText:[dataArray1 objectAtIndex:indexPath.row]];//給cell添加數據
-
- break;
-
- case 1:
-
- [[cell textLabel] setText:[dataArray2 objectAtIndex:indexPath.row]];
-
- break;
-
- default:
-
- [[cell textLabel] setText:@"Unknown"];
-
- }
-
- return cell;//返回cell
-
- }
上面的例子在功能上介紹了tableview的使用,但其在數據處理上具有很大的局限性。當我們要從服務器上請求數據,面對多種可能的數據(主要指數組的個數不穩定)此時上面的switch將無法滿足我們的需求了。使用switch可是代碼的結構清晰明了,但其局限性很致命(switch中case的個數無法動態指定),下面的另一種方法可解決上述問題。

代碼在原由基礎上進行的修改:
- @interface TableViewTestViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{
-
- UITableView *DataTable;
-
- NSMutableArray *dataArray1;
-
- NSMutableArray *dataArray2;
-
- NSMutableArray *titleArray;
-
- NSMutableArray *dataArray; //加入了用于保存數組的數組 dataArray
-
- }
-
-
-
- - (void)viewDidLoad
-
- {
-
- [superviewDidLoad];
-
- DataTable = [[UITableViewalloc] initWithFrame:CGRectMake(0, 0, 320, 420)];
-
- [DataTablesetDelegate:self];
-
- [DataTablesetDataSource:self];
-
- [self.viewaddSubview:DataTable];
-
-
-
- dataArray1 = [[NSMutableArrayalloc] initWithObjects:@"中國", @"美國", @"英國", nil];
-
- dataArray2 = [[NSMutableArrayalloc] initWithObjects:@"黃種人", @"黑種人", @"白種人", nil];
-
- titleArray = [[NSMutableArrayalloc] initWithObjects:@"國家", @"種族", nil];
-
- dataArray = [[NSMutableArrayalloc] initWithObjects:dataArray1, dataArray2, nil]; //初始化dataArray,元素為數組
-
-
-
- }
-
-
-
-
-
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
-
- {
-
- // Return YES for supported orientations
-
- return (interfaceOrientation == UIInterfaceOrientationPortrait);
-
- }
-
- //制定個性標題,這里通過UIview來設計標題,功能上豐富,變化多。
-
- - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
-
- UIView *view = [[[UIViewalloc] initWithFrame:CGRectMake(0, 0, 320, 40)] autorelease];
-
- [view setBackgroundColor:[UIColorbrownColor]];//改變標題的顏色,也可用圖片
-
- UILabel *label = [[UILabelalloc] initWithFrame:CGRectMake(5, 5, 100, 30)];
-
- label.textColor = [UIColorredColor];
-
- label.backgroundColor = [UIColorclearColor];
-
- label.text = [titleArrayobjectAtIndex:section];
-
- [view addSubview:label];
-
- return view;
-
- }
-
- //指定標題的高度
-
- - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
-
- return 40;
-
- }
-
-
-
- //每個section顯示的標題,有了上面的這個就不要了
-
- - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
-
- }
-
-
-
- //指定有多少個分區(Section),默認為1
-
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
-
- return [titleArraycount];
-
- }
-
-
-
- //指定每個分區中有多少行,默認為1
-
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
-
- /* switch (section) {
-
- case 0:
-
- return [dataArray1 count];
-
- break;
-
- case 1:
-
- return [dataArray2 count];
-
- break;
-
- default:
-
- return 0;
-
- break;
-
- }*/
-
- /* for(int i = 0; i < [titleArray count]; i++){
-
- if(section == i){
-
- return [[dataArray objectAtIndex:section] count];
-
- }
-
- }*/
-
- //上面的方法也是可行的,大家參考比較下
-
- return [[dataArray objectAtIndex:section] count]; //取dataArray中的元素,并根據每個元素(數組)來判斷分區中的行數。
-
-
-
-
-
- }
-
-
-
- //繪制Cell
-
- -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
-
-
-
- static NSString *CellIdentifier = @"Cell";
-
-
-
- UITableViewCell *cell = (UITableViewCell*)[tableView
-
- dequeueReusableCellWithIdentifier:CellIdentifier];
-
- if(cell == nil)
-
- {
-
- cell = [[[UITableViewCellalloc]
-
- initWithFrame:CGRectZero
-
- reuseIdentifier:CellIdentifier] autorelease];
-
- }
-
-
-
- /*switch (indexPath.section) {
-
- case 0:
-
- [[cell textLabel]
-
- setText:[dataArray1 objectAtIndex:indexPath.row]];
-
- break;
-
- case 1:
-
- [[cell textLabel]
-
- setText:[dataArray2 objectAtIndex:indexPath.row]];
-
- break;
-
- default:
-
- [[cell textLabel]
-
- setText:@"Unknown"];
-
- }*/
-
- //上面的方法也可行,大家比較下。
-
- [[cell textLabel] setText:[[dataArrayobjectAtIndex:indexPath.section]objectAtIndex:indexPath.row]];
-
- //同上,取出dataArray中每個分區所對應的元素(數組),并通過其來取值。 (大家要有想像力, 復制代碼試試就明白了)
-
-
-
- return cell;
-
-
-
- }
-
-
-
- //改變行的高度
-
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
-
- return40;
-
- }
轉自:http://www.cnblogs.com/top5/archive/2012/05/17/2506604.html

]]>
主站蜘蛛池模板:
崇左市|
曲靖市|
邯郸市|
淮北市|
松溪县|
武山县|
双桥区|
津南区|
同心县|
丽水市|
鹿邑县|
册亨县|
洛川县|
司法|
水城县|
城市|
呼图壁县|
嘉兴市|
松滋市|
平阳县|
鸡东县|
三明市|
农安县|
健康|
西安市|
卫辉市|
仪征市|
福海县|
金阳县|
原阳县|
西宁市|
武陟县|
西盟|
东源县|
巴林右旗|
文山县|
广西|
安吉县|
潮安县|
湄潭县|
辽宁省|