I know this is pretty easy, creating a table view inside a view. Always we must set the delegate and the data source of that table to self, else you will end up having just the tableview without any data in it.
[sourcecode language=”objc”]
// add the delegates to the interface as shown below
@interface sflyViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>
UITableView *tbview;
@property (nonatomic,retain) UITableView *tbview;
// in .m class
– (void)viewDidLoad
{
[super viewDidLoad];
tbview = [[UITableView alloc] initWithFrame:self.view.bounds];
[tbview setDelegate:self]; ***** important
[tbview setDataSource:self]; **** important
[self.view addSubview:tbview];
// override the below methods
#pragma mark Table view methods
– (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 20;
}
– (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
– (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if([jsonthumbnailsstring count] < 10)
return 20;
return [jsonthumbnailsstring count];
}
// Customize the appearance of table view cells.
– (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];
}
// Set up the cell…
cell.textLabel.font = [UIFont fontWithName:@”Helvetica” size:15];
cell.textLabel.text = [NSString stringWithFormat:@”Cell Row #%d”, [indexPath row]];
return cell;
}
– (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
//
return UITableViewCellEditingStyleDelete;
}
– (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// open a alert with an OK and cancel button
NSString *alertString = [NSString stringWithFormat:@”Clicked on row #%d”, [indexPath row]];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:alertString message:@”” delegate:self cancelButtonTitle:@”Done” otherButtonTitles:nil];
[alert show];
}
[/sourcecode]
Leave a Reply