· 8 years ago · Aug 13, 2018, 06:38 AM
1how reload table view content depend on segment index
2- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
3 static NSString *CellIdentifier = @"Cell";
4
5 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
6 if (cell == nil) {
7 cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
8 }
9
10 // Set up the cell
11 book *bok = (book *)[self.booktable objectAtIndex:indexPath.row];
12
13 NSArray *rowoftitle = [NSArray arrayWithObjects:bok.book_title, nil];
14 NSArray *rowofauth = [NSArray arrayWithObjects:bok.author_name, nil];
15 NSArray *rowofpub = [NSArray arrayWithObjects:bok.pub_name, nil];
16
17 //[cell setText:bok.book_title];
18 [tableView beginUpdates];
19
20 switch (segControl.selectedSegmentIndex)
21 {
22 case 0:
23 [tableView insertRowsAtIndexPaths:rowoftitle withRowAnimation:UITableViewRowAnimationTop];
24
25 //cell.textLabel.text =bok.book_title;
26 break;
27
28 case 1:
29 [tableView insertRowsAtIndexPaths:rowofauth withRowAnimation:UITableViewRowAnimationTop];
30
31
32 //[libraryTV reloadData];
33 //cell.textLabel.text =bok.author_name;
34 break;
35 case 2:
36 [tableView insertRowsAtIndexPaths:rowofpub withRowAnimation:UITableViewRowAnimationTop];
37
38 //cell.textLabel.text =bok.pub_name;
39 break;
40
41 default:
42 break;
43}
44
45 [tableView endUpdates];
46 //return cell;
47}
48
49-(void)segmentSelected:(UISegmentedControl *)sender
50{
51 switch (sender.selectedSegmentIndex)
52 {
53 case 0:
54 [self loadTitles];
55 break;
56 case 1:
57 [self loadAuthors];
58 break;
59 case 2:
60 [self loadPublishers];
61 break;
62 default:
63 break;
64 }
65}
66
67
68
69
70- (void) loadTitles
71{
72 [self.dataSourceArray removeAllObjects];
73 for (Book *book in self.booktable)
74 {
75 [dataSourceArray addObject:book.book_title];
76 }
77 [<yourTableViewVariable> reloadData];
78}
79
80- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
81{
82 return [dataSourceArray count];
83}
84
85- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
86 static NSString *CellIdentifier = @"Cell";
87
88 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
89 if (cell == nil) {
90 cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
91 }
92
93 // Set up the cell
94 cell.textLabel.text = [dataSourceArray objectAtIndex:indexPath.row];
95 return cell;
96}
97
98NSMutableArray *booktable;
99NSMutableArray *sbook;
100NSMutableArray *rowoflibrary;
101NSString *databaseName;
102NSString *databasePath;
103IBOutlet UISegmentedControl *segControl;
104IBOutlet UITableView *libraryTV;
105IBOutlet UISearchBar *searchBar;
106
107- (void)viewDidLoad {
108
109
110databaseName = @"MydataBase.db";
111
112// Get the path to the documents directory and append the databaseName
113NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
114NSString *documentsDir = [documentPaths objectAtIndex:0];
115databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
116
117// Execute the "checkAndCreateDatabase" function
118[self checkAndCreateDatabase];
119
120[self readbookFromDatabase];
121
122
123booktable = [[NSMutableArray alloc]init];
124
125[booktable addObjectsFromArray:sbook];
126
127rowoflibrary = [[NSMutableArray alloc] init];
128
129[super viewDidLoad];}
130
131
132-(void) checkAndCreateDatabase{
133// Check if the SQL database has already been saved to the users phone, if not then copy it over
134BOOL success;
135
136// Create a FileManager object, we will use this to check the status
137// of the database and to copy it over if required
138NSFileManager *fileManager = [NSFileManager defaultManager];
139
140// Check if the database has already been created in the users filesystem
141success = [fileManager fileExistsAtPath:databasePath];
142
143// If the database already exists then return without doing anything
144if(success) return;
145
146// If not then proceed to copy the database from the application to the users filesystem
147
148// Get the path to the database in the application package
149NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
150
151// Copy the database from the package to the users filesystem
152[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
153
154[fileManager release];}
155
156-(void) readbookFromDatabase {
157// Setup the database object
158sqlite3 *database;
159
160// Init the staff Array
161sbook = [[NSMutableArray alloc] init];
162
163// Open the database from the users filessytem
164if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
165 // Setup the SQL Statement and compile it for faster access
166
167 const char *sqlStatement ="select book.title,author.auth_name ,publisher.pub_name , book.info from book ,author, publisher where book.auth_id = author.auth_id and book.pub_id = publisher.pub_id";
168
169
170 sqlite3_stmt *compiledStatement;
171 if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
172 // Loop through the results and add them to the feeds array
173 while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
174 // Read the data from the result row
175
176 char * str = (char*)sqlite3_column_text(compiledStatement, 0);
177 if (str){
178 title = [NSString stringWithUTF8String:str];
179 }
180 else{
181 title =@" ";
182 }
183
184 char * str1 = (char*)sqlite3_column_text(compiledStatement, 1);
185 if (str1){
186 authorName = [NSString stringWithUTF8String:str1];
187 }
188 else{
189 authorName =@" ";
190 }
191
192 char * str2 = (char*)sqlite3_column_text(compiledStatement, 2);
193 if (str2){
194 pubName = [NSString stringWithUTF8String:str2];
195 }
196 else{
197 pubName =@" ";
198 }
199
200 char * str3 = (char*)sqlite3_column_text(compiledStatement,3);
201 if (str3){
202 info = [NSString stringWithUTF8String:str3];
203 }
204 else{
205 info =@" ";
206 }
207
208 // Create a new book object with the data from the database
209
210 book *bok = [[book alloc] initWithbook:title autname:authorName pubname:pubName infobook:info];
211
212
213 // Add the book object to the staff Array
214 [sbook addObject:bok];
215
216 [bok release];
217 }
218 }
219 // Release the compiled statement from memory
220 sqlite3_finalize(compiledStatement);
221
222}
223sqlite3_close(database);}
224
225for (book *bok in self.booktable)
226{
227 [rowoflibrary addObject:bok.book_title];
228}
229[libraryTV reloadData];
230
231return [rowoflibrary count];
232
233return cell;