我的表视图更改内容取决于SegmentIndex
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell
book *bok = (book *)[self.booktable objectAtIndex:indexPath.row];
NSArray *rowoftitle = [NSArray arrayWithObjects:bok.book_title, nil];
NSArray *rowofauth = [NSArray arrayWithObjects:bok.author_name, nil];
NSArray *rowofpub = [NSArray arrayWithObjects:bok.pub_name, nil];
//[cell setText:bok.book_title];
[tableView beginUpdates];
switch (segControl.selectedSegmentIndex)
{
case 0:
[tableView insertRowsAtIndexPaths:rowoftitle withRowAnimation:UITableViewRowAnimationTop];
//cell.textLabel.text =bok.book_title;
break;
case 1:
[tableView insertRowsAtIndexPaths:rowofauth withRowAnimation:UITableViewRowAnimationTop];
//[libraryTV reloadData];
//cell.textLabel.text =bok.author_name;
break;
case 2:
[tableView insertRowsAtIndexPaths:rowofpub withRowAnimation:UITableViewRowAnimationTop];
//cell.textLabel.text =bok.pub_name;
break;
default:
break;
}
[tableView endUpdates];
//return cell;
}
但是当它运行时,就会发生赎罪:
'NSInvalidArgumentException',原因:' - [NSCFString row]:无法识别 选择器发送到实例0x5d84f80
如何解决它。
答案 0 :(得分:1)
一种解决方案可能是:
您将需要使用公共数据源(在此代码中我使用dataSourceArray
来实现此目的,您需要在 header 文件和alloc / init中声明它在viewDidLoad
方法中。),将在更改细分时进行更新。
它将具有以下方法。(用于段更改事件)
-(void)segmentSelected:(UISegmentedControl *)sender
{
switch (sender.selectedSegmentIndex)
{
case 0:
[self loadTitles];
break;
case 1:
[self loadAuthors];
break;
case 2:
[self loadPublishers];
break;
default:
break;
}
}
- (void) loadTitles
{
[self.dataSourceArray removeAllObjects];
for (Book *book in self.booktable)
{
[dataSourceArray addObject:book.book_title];
}
[<yourTableViewVariable> reloadData];
}
类似于其他方法的代码loadAuthors
和loadPublishers
。
现在您的numberOfRowsInSection
方法将是:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [dataSourceArray count];
}
和cellForRowAtIndexPath
将是:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell
cell.textLabel.text = [dataSourceArray objectAtIndex:indexPath.row];
return cell;
}
由于
答案 1 :(得分:0)
更改内容意味着根据部分从数据源设置单元格的属性。这是正常的,Apple关于如何实现UITableViewController
的文档很好地涵盖了它。您正在做的是尝试在表重新加载时修改表的结构。你绝对不能那样做。
表修改方法(例如insertRowsAtIndexPaths:withRowAnimation:
)是在您修改数据模型并想要动态更新表而无需重新加载整个表之后调用的。这些不能在用于构造表或单元格的任何方法中调用。 tableView:cellForRowAtIndexPath:
将最终调用insertRowsAtIndePaths:...
来构建新行的单元格。
答案 2 :(得分:0)
:
NSMutableArray *booktable;
NSMutableArray *sbook;
NSMutableArray *rowoflibrary;
NSString *databaseName;
NSString *databasePath;
IBOutlet UISegmentedControl *segControl;
IBOutlet UITableView *libraryTV;
IBOutlet UISearchBar *searchBar;
在实施中
- (void)viewDidLoad {
databaseName = @"MydataBase.db";
// Get the path to the documents directory and append the databaseName
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
// Execute the "checkAndCreateDatabase" function
[self checkAndCreateDatabase];
[self readbookFromDatabase];
booktable = [[NSMutableArray alloc]init];
[booktable addObjectsFromArray:sbook];
rowoflibrary = [[NSMutableArray alloc] init];
[super viewDidLoad];}
-(void) checkAndCreateDatabase{
// Check if the SQL database has already been saved to the users phone, if not then copy it over
BOOL success;
// Create a FileManager object, we will use this to check the status
// of the database and to copy it over if required
NSFileManager *fileManager = [NSFileManager defaultManager];
// Check if the database has already been created in the users filesystem
success = [fileManager fileExistsAtPath:databasePath];
// If the database already exists then return without doing anything
if(success) return;
// If not then proceed to copy the database from the application to the users filesystem
// Get the path to the database in the application package
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
// Copy the database from the package to the users filesystem
[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
[fileManager release];}
-(void) readbookFromDatabase {
// Setup the database object
sqlite3 *database;
// Init the staff Array
sbook = [[NSMutableArray alloc] init];
// Open the database from the users filessytem
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
// Setup the SQL Statement and compile it for faster access
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";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
// Loop through the results and add them to the feeds array
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
// Read the data from the result row
char * str = (char*)sqlite3_column_text(compiledStatement, 0);
if (str){
title = [NSString stringWithUTF8String:str];
}
else{
title =@" ";
}
char * str1 = (char*)sqlite3_column_text(compiledStatement, 1);
if (str1){
authorName = [NSString stringWithUTF8String:str1];
}
else{
authorName =@" ";
}
char * str2 = (char*)sqlite3_column_text(compiledStatement, 2);
if (str2){
pubName = [NSString stringWithUTF8String:str2];
}
else{
pubName =@" ";
}
char * str3 = (char*)sqlite3_column_text(compiledStatement,3);
if (str3){
info = [NSString stringWithUTF8String:str3];
}
else{
info =@" ";
}
// Create a new book object with the data from the database
book *bok = [[book alloc] initWithbook:title autname:authorName pubname:pubName infobook:info];
// Add the book object to the staff Array
[sbook addObject:bok];
[bok release];
}
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);}
- (void)segmentSelected:(UISegmentedControl *)sender { switch(sender.selectedSegmentIndex){ 案例0: [self loadTitles]; 打破; 情况1: [self loadAuthors]; 打破; 案例2: [self loadPublishers]; 打破; 默认: 打破; }} - (void)loadTitles { [rowoflibrary removeAllObjects];
for (book *bok in self.booktable)
{
[rowoflibrary addObject:bok.book_title];
}
[libraryTV reloadData];
}
(void)loadAuthors {
[rowoflibrary removeAllObjects];
for(book * bok in self.booktable) { [rowoflibrary addObject:bok.author_name]; } [libraryTV reloadData];
}
(void)loadPublishers {
[rowoflibrary removeAllObjects];
for(book * bok in self.booktable) { [rowoflibrary addObject:bok.pub_name]; } [libraryTV reloadData];
}
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [rowoflibrary count];
}
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString * CellIdentifier = @“Cell”;
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if(cell == nil){ cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
//设置单元格
cell.textLabel.text = [rowoflibrary objectAtIndex:indexPath.row];
return cell;
}
运行时,它是空表视图