Skip to content

Instantly share code, notes, and snippets.

@wangdu1005
Created November 15, 2017 09:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wangdu1005/38ee4add905070306a97cfcc27b73177 to your computer and use it in GitHub Desktop.
Save wangdu1005/38ee4add905070306a97cfcc27b73177 to your computer and use it in GitHub Desktop.
//
// TableViewController.m
// testProject
//
// Created by WZ on 03/10/2017.
// Copyright © 2017 zeus. All rights reserved.
//
#import "DeviceInfoTableViewController.h"
@interface DeviceInfoTableViewController ()
// @property (nonatomic, strong) NSArray *tableData;
@end
@implementation DeviceInfoTableViewController
{
UITableView *tableView;
NSArray *tableData;
NSMutableArray *aMutableArray;
NSMutableString *rowValue;
}
- (void)viewDidLoad
{
[super viewDidLoad];
rowValue = [NSMutableString stringWithString:@"Taiwan"];
NSString *values[13];
values[0] = @"Eezy";
values[1] = @"Tutorials";
values[2] = @"Website"; // Website ignored since count is 2
values[3] = @"Tutorials4";
values[4] = @"Tutorials5";
values[5] = @"Tutorials6";
values[6] = @"Tutorials7";
values[7] = @"Tutorials8";
values[8] = @"Tutorials9";
values[9] = @"Tutorials10";
values[10] = @"Tutorials11";
values[11] = @"Tutorials12";
values[12] = @"Tutorials13";
tableData = [NSArray arrayWithObjects:values count:13];
NSLog(@"%@",tableData);
aMutableArray = [NSMutableArray arrayWithArray:tableData];
tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
// must set delegate & dataSource, otherwise the the table will be empty and not responsive
tableView.delegate = self;
tableView.dataSource = self;
tableView.backgroundColor = [UIColor cyanColor];
// add to canvas
[self.view addSubview:tableView];
}
#pragma mark - UITableViewDataSource
// number of section(s), now I assume there is only 1 section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)theTableView
{
return 1;
}
// number of row in the section, I assume there is only 1 row
- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section
{
// return 1;
// return [tableData count];
// return (tableData.count - 1);
NSLog(@"aMutableArray count: %ld", (unsigned long)[aMutableArray count]);
return [aMutableArray count];
// return 50;
}
// the cell will be returned to the tableView
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
// Similar to UITableViewCell, but
UITableViewCell *cell = (UITableViewCell *)[theTableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
// Just want to test, so I hardcode the data
cell.textLabel.text = @"Testing";
NSLog(@"aMutableArray count: %ld", (unsigned long)[aMutableArray count]);
NSLog(@"selected cell rowIndex: %ld", (long)indexPath.row);
// These code below will crash the app
// NSInteger row = indexPath.row;
// rowValue = [aMutableArray objectAtIndex:row];
// NSLog(@"selected cell rowValue: %@", rowValue);
// cell.textLabel.text = rowValue;
return cell;
}
#pragma mark - UITableViewDelegate
// when user tap the row, what action you want to perform
- (void)tableView:(UITableView *)theTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSLog(@"selected %ld row", (long)indexPath.row);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment