我的表格中有三个部分.each部分包含我需要根据打开的部分搜索值的不同数组。 数组是: 1.Colors 2.Numbers 3.Alaphabet
请帮助我,我是iOS新手...提前给我解决方案...
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
for (UIView *view in cell.contentView.subviews) {
[view removeFromSuperview];
}
switch (indexPath.section) {
case 0:
cell.textLabel.text =self.colors[indexPath.row];
break;
case 1:
cell.textLabel.text =self.numbers[indexPath.row];
break;
case 2:
cell.textLabel.text =self.alpha[indexPath.row];
break;
default:
break;
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 50;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIButton *titleButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
titleButton.frame = CGRectMake(0, 0, 320, 30);
[titleButton setBackgroundColor:[UIColor clearColor]];
[titleButton addTarget:self action:@selector(handleTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
titleButton.tag = section;
NSString *sectionName;
switch (section)
{
case 0: sectionName = NSLocalizedString(@"Colors", @"Colors");
break;
case 1: sectionName = NSLocalizedString(@"Numbers", @"Numbers");
break;
case 2: sectionName = NSLocalizedString(@"Alabaphet", @"Alabaphet");
break;
default: sectionName = @"";
break;
}
[titleButton setTitle:sectionName forState:UIControlStateNormal];
return titleButton;
}
-(void)handleTouchUpInside:(UIButton *)sender
{
if(selectedIndex== sender.tag)
{
selectedIndex=50;
}
else
{
selectedIndex=sender.tag;
}
[menuTableView reloadData];
}
答案 0 :(得分:0)
快速的核心数据代码3
func checkDataInCD(entityName:String,keyName:String) -> Array<Any> {
var temp = Array<Any>()
do{
//getting all datas from entity
let request = self .setFetchReques(entityName: entityName)
request.predicate = self .setPredicate(keyName: keyName, entityName:entityName)
temp = try self.getContext().fetch(request) as Array
if temp .count == 0 {
return temp
}else{
return temp
}
}catch{
// print("problem with fetching core data")
}
return temp
}
func getContext() -> NSManagedObjectContext {
let context = (UIApplication .shared .delegate as! AppDelegate).persistentContainer.viewContext
return context
}
func setFetchReques(entityName:String) -> NSFetchRequest<NSFetchRequestResult> {
let request = NSFetchRequest<NSFetchRequestResult>(entityName:entityName)
request .returnsObjectsAsFaults = false
return request
}
func setPredicate(keyName:String, entityName:String) -> NSPredicate {
let resultPredicate = NSPredicate(format: "key = %@", keyName)
let request = NSFetchRequest<NSFetchRequestResult>(entityName: entityName)
request.predicate = resultPredicate
return resultPredicate
}
func deleteDataInCD(entityName : String, keyName: String) {
do{
let request = self .setFetchReques(entityName: entityName)
request.predicate = self .setPredicate(keyName: keyName, entityName:entityName)
let results = try self.getContext().fetch(request) as Array
// print(entityName)
// print(keyName)
// print(results.count)
if results.count > 0{
for result in results as! [NSManagedObject]{
if result.value(forKey: "data") != nil {
self.getContext().delete(result)
}
do{
try self .getContext().save()
} catch{
// print("error deleting data from core data")
}
}
}
} catch{
// print("no records found")
}
}
self.deleteDataInCD(entityName: "NewsListing", keyName: self.pageType)
let menuObj = NewsListingModel(categories:self.dataArray)
let newsListing = NewsListing(context: self.getContext())
let encodedObject = NSKeyedArchiver.archivedData(withRootObject:menuObj) as NSObject?
newsListing.data = encodedObject
newsListing.key = self.pageType
(UIApplication .shared .delegate as! AppDelegate).saveContext()
import UIKit
class NewsListingModel: NSObject,NSCoding {
var categories: [[String:Any]]
init(categories:[[String:Any]]) {
self.categories = categories
}
// MARK: NSCoding
required convenience init(coder aDecoder: NSCoder) {
let categories = aDecoder.decodeObject(forKey: "categories")
self.init(categories: categories! as! [[String:Any]])
}
func encode(with aCoder: NSCoder) {
// print(self.categories)
aCoder.encode(self.categories, forKey: "categories")
//aCoder.encodeConditionalObject(self.categories, forKey: "categories")
}
}