Iphone将tableview与nib文件按行分组,我的行在每个部分重新启动,它不会显示下一部分行
我从SDK开始,尝试使用tableview设置一个应用程序来显示信息 我创建了一个基于窗口的应用程序,我使用了一个带有tableview的选项卡栏,该选项卡栏中有一个导航栏,可以来回移动,它“显然”工作得很好,但我遇到了一个问题,每当你点击每个(1,2,3…)节的第一行时,它就会不断调用第一节的第一行,我做了很多研究:教程、书籍、论坛,但似乎找不到办法让它发挥作用。我发现了很多有用的信息,虽然这对我帮助很大,但并不能解决我的问题。我已经适应它好几天了 因此,在尝试了我能找到的所有资源后,我向你呼救 这是我试图使用的代码,我想它会对你们中的一些人有所帮助 //我正在用这个 MyTableViewController.h{Iphone将tableview与nib文件按行分组,我的行在每个部分重新启动,它不会显示下一部分行,iphone,objective-c,ios-simulator,Iphone,Objective C,Ios Simulator,我从SDK开始,尝试使用tableview设置一个应用程序来显示信息 我创建了一个基于窗口的应用程序,我使用了一个带有tableview的选项卡栏,该选项卡栏中有一个导航栏,可以来回移动,它“显然”工作得很好,但我遇到了一个问题,每当你点击每个(1,2,3…)节的第一行时,它就会不断调用第一节的第一行,我做了很多研究:教程、书籍、论坛,但似乎找不到办法让它发挥作用。我发现了很多有用的信息,虽然这对我帮助很大,但并不能解决我的问题。我已经适应它好几天了 因此,在尝试了我能找到的所有资源后,我向你呼
#import <UIKit/UIKit.h>
#import "firstCustomCell.h"
@interface MyTableViewController : UITableViewController <UITableViewDelegate,UITableViewDataSource> {
IBOutlet UITableView *theTableView;
NSMutableArray *firstArray;
NSMutableArray *secondArray;
NSMutableArray *thirdArray;
NSMutableArray *listOfItems;
}
@property (nonatomic, retain) NSMutableArray *firstArray;
@property (nonatomic, retain) NSMutableArray *secondArray;
@property (nonatomic, retain) NSMutableArray *thirdArray;
@property (nonatomic, retain) NSMutableArray *listOfItems;
@end
}
//自定义表格视图单元格的外观
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"ScustomCell";
firstCustomCell *cell = (firstCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSLog(@"Cell created");
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"firstCustomCell" owner:nil options:nil];
for(id currentObject in nibObjects) {
if ([currentObject isKindOfClass:[firstCustomCell class]]) {
cell = (firstCustomCell *)currentObject;
}
}
}
switch (indexPath.row) {
case 0:
(indexPath.section == 0, indexPath.row == 0);
[[cell currentNameLabel] setText:@"Week One"];
[[cell imageView] setImage:[UIImage imageNamed:@"1.png"]];
break;
case 1:
(indexPath.section == 0, indexPath.row == 1);
[[cell currentNameLabel] setText:@"Week Two"];
[[cell imageView] setImage:[UIImage imageNamed:@"2.png"]];
break;
case 2:
(indexPath.section == 0, indexPath.row %12 == 2);
[[cell currentNameLabel] setText:@"Week Three"];
[[cell imageView] setImage:[UIImage imageNamed:@"3.png"]];
break;
case 3:
(indexPath.section == 1, indexPath.row %12 == 0);
[[cell currentNameLabel] setText:@"Week Four"];
[[cell imageView] setImage:[UIImage imageNamed:@"4.png"]];
break;
case 4:
(indexPath.section == 1, indexPath.row %12 == 1);
[[cell currentNameLabel] setText:@"Week Five"];
[[cell imageView] setImage:[UIImage imageNamed:@"5.png"]];
break;
case 5:
(indexPath.section == 1, indexPath.row %12 == 2);
[[cell currentNameLabel] setText:@"Week Six"];
[[cell imageView] setImage:[UIImage imageNamed:@"6.png"]];
break;
case 6:
(indexPath.section == 1, indexPath.row %12 == 3);
[[cell currentNameLabel] setText:@"Week Seven"];
[[cell imageView] setImage:[UIImage imageNamed:@"7.png"]];
break;
case 7:
(indexPath.section == 2, indexPath.row %12 == 0);
[[cell currentNameLabel] setText:@"Week Eight"];
[[cell imageView] setImage:[UIImage imageNamed:@"8.png"]];
break;
case 8:
(indexPath.section == 2, indexPath.row %12 == 1);
[[cell currentNameLabel] setText:@"Week Nine"];
[[cell imageView] setImage:[UIImage imageNamed:@"9.png"]];
break;
case 9:
(indexPath.section == 2, indexPath.row %12 == 2);
[[cell currentNameLabel] setText:@"Week Ten"];
[[cell imageView] setImage:[UIImage imageNamed:@"10.png"]];
break;
case 10:
(indexPath.section == 2, indexPath.row %12 == 3);
[[cell currentNameLabel] setText:@"Week Eleven"];
[[cell imageView] setImage:[UIImage imageNamed:@"11.png"]];
break;
case 11:
(indexPath.section == 2, indexPath.row %12 == 4);
[[cell currentNameLabel] setText:@"Week Twelve"];
[[cell imageView] setImage:[UIImage imageNamed:@"12.png"]];
break;
}
//配置单元格
NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:@"CompoNente"];
NSString *cellValue = [array objectAtIndex:indexPath.row];
cell.text = cellValue; //also receive a warning that setText is deprecated.
return cell;
}
//然后是代表
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if ([[firstArray objectAtIndex:indexPath.row] isEqual: @"Week One"]) {
One *one = [[One alloc]initWithNibName:@"One" bundle:nil];
[self.navigationController pushViewController:one animated:YES];
[one release];
}
else if ([[firstArray objectAtIndex:indexPath.row] isEqual:@"Week Two"]) {
Two *two = [[Two alloc]initWithNibName:@"Two" bundle:nil];
[self.navigationController pushViewController:two animated:YES];
[two release];
}
else if ([[firstArray objectAtIndex:indexPath.row] isEqual:@"Week Three"]) {
Three *three = [[Three alloc]initWithNibName:@"Three" bundle:nil];
[self.navigationController pushViewController:three animated:YES];
[three release];
}
else if ([[firstArray objectAtIndex:indexPath.row] isEqual:@"Week Four"]) {
Four *four = [[Four alloc]initWithNibName:@"Four" bundle:nil];
[self.navigationController pushViewController:four animated:YES];
[four release];
}
else if ([[firstArray objectAtIndex:indexPath.row] isEqual:@"Week Five"]) {
Five *five = [[Five alloc]initWithNibName:@"Five" bundle:nil];
[self.navigationController pushViewController:five animated:YES];
[five release];
}
else if ([[firstArray objectAtIndex:indexPath.row] isEqual:@"Week Six"]) {
Six *six = [[Six alloc]initWithNibName:@"Six" bundle:nil];
[self.navigationController pushViewController:six animated:YES];
[six release];
}
else if ([[firstArray objectAtIndex:indexPath.row] isEqual:@"Week Seven"]) {
Seven *seven = [[Seven alloc]initWithNibName:@"Seven" bundle:nil];
[self.navigationController pushViewController:seven animated:YES];
[seven release];
}
else if ([[firstArray objectAtIndex:indexPath.row] isEqual:@"Week Eight"]) {
Eight *eight = [[Eight alloc]initWithNibName:@"Eight" bundle:nil];
[self.navigationController pushViewController:eight animated:YES];
[eight release];
}
else if ([[firstArray objectAtIndex:indexPath.row] isEqual:@"Week Nine"]) {
Nine *nine = [[Nine alloc]initWithNibName:@"Nine" bundle:nil];
[self.navigationController pushViewController:nine animated:YES];
[nine release];
}
else if ([[firstArray objectAtIndex:indexPath.row] isEqual:@"Week Ten"]) {
Ten *ten = [[Ten alloc]initWithNibName:@"Ten" bundle:nil];
[self.navigationController pushViewController:ten animated:YES];
[ten release];
}
else if ([[firstArray objectAtIndex:indexPath.row] isEqual:@"Week Eleven"]) {
Eleven *eleven = [[Eleven alloc]initWithNibName:@"Eleven" bundle:nil];
[self.navigationController pushViewController:eleven animated:YES];
[eleven release];
}
else if ([[firstArray objectAtIndex:indexPath.row] isEqual:@"Week Twelve"]) {
Twelve *twelve = [[Twelve alloc]initWithNibName:@"Twelve" bundle:nil];
[self.navigationController pushViewController:twelve animated:YES];
[twelve release];
}
}
//添加节的标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section == 0)
return @"FIRST SECTION";
else if (section == 1)
return @"SECOND SECTION";
else if (section == 2)
return @"THIRD SECTION";
} // also receive a warning "Control reaches end of non-void function"
//释放或清理内存并完成TableView
#pragma mark -
#pragma mark Memory management
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Relinquish ownership any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
}
- (void)dealloc {
[listOfItems release];
[thirdArray release];
[secondArray release];
[firstArray release];
[super dealloc];
}
@end
是的,我总是有这个问题-我不确定它是什么,但我认为这与以下事实有关:
==0
也是==NO
或==false
,但我不确定
通常对我有效的方法是将其视为整数,因此:
if([indexPath section] < 1){
// section 0
}
else if([indexPath section] > 0 && [indexPath section] < 2){
// section 1
}
else{
// etc...
}
然后进行字符串比较:
if([section isEqualToString:@"0"]){
// first section
}
else if([section isEqualToString:@"1"]){
// next section
}
else {
// etc...
}
在-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(nsindepath*)indepath
中,您只是忽略了indepath.section。没有考虑该节的调用
您的分区标题是否有效?对我来说,这看起来没问题。
添加一个
return nil;
添加此方法的结尾以消除此警告。之所以会出现此警告,是因为如果节大于2,则不会返回任何内容
嗯,你真的在10周内创建了10个不同的类和10个不同的nib文件吗?我怀疑这是否真的有必要。如果你的客户希望在下一个版本中获得52周的支持,这将使你陷入大量的复制和粘贴;-)对不起,你到底是在实现这些方法吗???感觉很好,但是…哦,对不起-在您看到的任何表视图方法中,
something…forrowatinexpath:(nsindepath*)indexPath
您可以使用此代码。我正在我的手机中尝试切换时的rowatindexpath,但没有成功。不要担心感觉noob-:p我们都在那里过一次,说实话,很难理解和理解。你说问题是当你点击一行时?输入didselectrowatindexpath实际上是12几个星期,但我需要他们在里面加载另一个tableview,以显示更多数据,并分别转到其他4或5个视图(lol)。如果你能告诉我一个不同的方式,我将不胜感激。是的,我明白你的意思,这将是一项繁重的工作,因此如果你现在有更好的方式,请分享…..谢谢。是的,我的标题有效,但感谢你,我不再收到警告。你是对的,我没有返回任何内容,现在我返回零;警告消失。我会的我很想给你们两个理由,因为你们两个都帮我解决了这件事,我确实把两个答案放在了一起,现在它真的起作用了。到达那里让我很震惊,但祈祷和听从你们的建议最终做到了。FluchPunkt你帮我意识到我没有在我的didSelectRowAtIndexPath m中调用这些部分ethod,并帮助我摆脱标题警告,我非常感谢你所拥有的这些人。但正是Thomas Clayson记得我如何实现该方法,(还有我的朋友Bucky,来自Youtube,他是objective C training。还有许多其他人。感谢大家。)所以我决定给你们两个最好的答案,但由于我无法做到这一点,我把它交给了托马斯·克莱森。但你们都是对的,我感谢你们的时间和关心。我明天再发。
NSString *section = [NSString stringWithFormat:@"%i",[indexPath section]];
if([section isEqualToString:@"0"]){
// first section
}
else if([section isEqualToString:@"1"]){
// next section
}
else {
// etc...
}
if ([[firstArray objectAtIndex:indexPath.row] isEqual: @"Week One"]) {
One *one = [[One alloc]initWithNibName:@"One" bundle:nil];
[self.navigationController pushViewController:one animated:YES];
[one release];
}
else if ([[firstArray objectAtIndex:indexPath.row] isEqual:@"Week Two"]) {
Two *two = [[Two alloc]initWithNibName:@"Two" bundle:nil];
[self.navigationController pushViewController:two animated:YES];
[two release];
}