我目前正在创建我的iPhone应用程序的一部分,基本上有一个单元格列表(在tableview中),这就像现有的苹果记事本一样。如何在iOS编程中获得每一枚枚举?
我想让它的单元格的名称有数组中的字符串的名称。这是我目前正在做的。
@interface ViewController()
{
NSMutableArray *cameraArray;
NSMutableArray *notesArray;
NSMutableArray *voiceArray;
}
@end
@implementation ViewController
//@synthesize myTableView;
- (void)viewDidLoad
{
[super viewDidLoad];
NSUserDefaults *ud=[NSUserDefaults standardUserDefaults];
//[ud setObject:@"Archer" forKey:@"char1class"];
[ud synchronize];
NSString *key1;//[ud stringForKey:@"Key1"];
NSString *key2; //[ud stringForKey:@"Key1"];
NSString *key3; //[ud stringForKey:@"Key1"];
if([ud stringForKey:@"Key1"] == nil){
key1 = @"Open Camera Slot";
}else{
key1 = [ud stringForKey:@"key1"];
}
if([ud stringForKey:@"Key2"] == nil){
key2 = @"Open Camera Slot";
}else{
key2 = [ud stringForKey:@"key2"];
}
if([ud stringForKey:@"Key3"] == nil){
key3 = @"Open Camera Slot";
}else{
key3 = [ud stringForKey:@"key3"];
}
cameraArray = [[NSMutableArray alloc]initWithObjects:key1, key2, key3, nil];
}
//tableview datasource delegate methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return cameraArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if(cell == nil){
cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
NSEnumerator *enumerator = [cameraArray objectEnumerator];
id anObject;
NSString *cellName = nil;
while (anObject = [enumerator nextObject]) {
cellName = anObject;
}
//static NSString *cellName = [cameraArray.objectAtIndex];
cell.textLabel.text = [NSString stringWithFormat:cellName];
return cell;
}
所以我基本上是从键在创建NSUserDefaults的在cameraArray字符串(我只是做这个测试的目的,字符串将用户输入以后)
我卡上的是什么枚举器遍历数组很好,但只使用tableView中所有单元格的最后一个值(第三个值)。
因此,如果有三根弦,“第一”“第二”和“第三”所有这三种细胞的说,“第三”
我该如何解决这个问题?
您总是将当前枚举值分配给cellName,因此每次cellName值都被重置。另外,你想完成什么? – aqua
你为什么要进行枚举?这是一个完全不必要的步骤:cell.textLabel.text = [cameraArray objectAtIndex:indexPath.row]; – rdelmar
感谢rdelmar,解决了我的问题 – tyler53