- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在两个 View 之间沟通时遇到了麻烦。我的目标是向我的 NSMutableArray
添加一个新对象.感谢 stackoverflow 上可爱的人,他们已经修复了。现在我有另一个问题。
即使我能够将对象添加到我的 NSMutableArray
,表不会更新自身以使用新数据重新填充。这是一些代码:
第一 View Controller .h
@interface FirstViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>{
IBOutlet UITableView *mytableview;
NSMutableArray *mytableinfo;
@property (nonatomic,retain) IBOutlet UITableView *mytableview;
@property (nonatomic,retain) NSMutableArray *mytableinfo;`
-(IBAction)addShift:(id)sender{
SecondViewController *secondViewController = [[SecondViewController alloc]init];
[self presentModalViewController:secondViewController animated:YES];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [mytableinfo count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
cell.textLabel.text =[mytableinfo objectAtIndex:indexPath.row];
NSLog(@"Data was written to table");
return cell;
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
mytableinfo = [[NSMutableArray alloc]init];
mytableview = [[UITableView alloc]init];
[mytableinfo addObject:@"Hello world1"];
[self.mytableview reloadData];
}
- (void)viewDidLoad
{
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addShift:)];
self.navigationItem.rightBarButtonItem = addButton;
[super viewDidLoad];
UIBarButtonItem *refresh = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(reloadTableData:)];
self.navigationItem.leftBarButtonItem = refresh;
mytableinfo = [[NSMutableArray alloc]init];
[mytableinfo addObject:@"First Shift"];
[self.mytableview reloadData];
// Do any additional setup after loading the view from its nib.
}
#import "SecondViewController.h"
#import "FirstViewController.h"
-(IBAction)saveShift:(id)sender{
FirstViewController *firstViewController = [[FirstViewController alloc]init];
firstViewController.mytableinfo = [[NSMutableArray alloc]init];
[firstViewController.mytableinfo addObject:@"SecondViwController Dismissed"];
NSLog(@"%@",[firstViewController.mytableinfo objectAtIndex:0]);
[self dismissModalViewControllerAnimated:YES];
}
saveShift
被称为我应该被带回主视图 - 我这样做 - 然后重新填充我的
tableview
.就好像我摆脱了一个小问题却跳入了另一个问题——感觉不是很好!
最佳答案
在您的 saveShift:(id)sender
方法你有很多代码。
只需执行以下操作:
[self.myTableInfo addObject:@"SecondViewConroller dimissed"];
[self dismissModalViewControllerAnimated:YES];
saveShift
在第二个 View 中,您目前正在创建
brand new
FirstView,这与您来自哪里不同。因此,当您关闭模态视图时,您的“全新”firstView 会丢失并且您会回到原来的 firstView。最后一个还没有听说过你的对象。
// SecondViewController.m
#import "SecondViewController.h"
#import "FirstViewController.h"
@implementation SecondViewController
@synthesize dateformatter,mydatepicker,startingTime;
// HERE Add a new property that match this and a variable in the .h
@synthesize array4Test;
// HERE make an init that will make this UIViewController know about your NSMutableArray
- (id)initWithMutableArray:aMutableArray
{
self = [self initWithNibName:nil bundle:nil];
if (self)
{
self.array4Test = aMutableArray;
}
return self;
}
- (void)dealloc
{
// HERE clean up
self.array4Test = nil;
[super dealloc];
}
-(IBAction)saveShift:(id)sender{
// HERE remove this code
//FirstViewController *firstViewController = [[FirstViewController alloc]init];
//[firstViewController.mytableinfo addObject:@"Hello world"];
//NSLog(@"%@",[firstViewController.mytableinfo objectAtIndex:0]);
// HERE add an object to the mutableArray that is store in your firstViewController, the one you've passed the reference in
[self.array4Test addObject:@"This is a String"];
[self dismissModalViewControllerAnimated:YES];
}
// FirstViewController.m
#import "FirstViewController.h"
#import "SecondViewController.h"
@implementation FirstViewController
@synthesize mytableinfo,mytableview;
@synthesize goneToOtherView;
-(IBAction)addShift:(id)sender{
SecondViewController *secondViewController = [[SecondViewController alloc] initWithMutableArray:self.mytableinfo];
[self presentModalViewController:secondViewController animated:YES];
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.mytableview reloadData];
NSString *aString = [mytableinfo lastObject];
if (aString)
{
NSLog(@"This just came back from the second View\n%@", aString);
}
}
关于ios - 未调用 cellForRowAtIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8029509/
我有一个问题,当从 tableView:cellForRowAtIndexPath 调用时,cellForRowAtIndexPath 总是返回 nil: 问题是我实际上是在 tableView:ce
假设 self.tableView 是我创建的 UITableView 实例变量,任何人都可以解释这两个语句之间的区别吗?两者在语法上都是正确的,而且我认为两者都会做同样的事情:在 self.tabl
我对 iPhone 开发还很陌生。因此,如果我问一些非常简单的问题,请耐心等待。 在我的应用程序中,我有多个 View (即 .xib 文件)。单击主视图(CouponWebsiteViewContr
Application Specific Information: *** Terminating app due to uncaught exception 'NSRangeException',
我正在显示通过 Storyboard 初始化的动态表格 View 。 没什么特别的,只是用文本标签初始化 3 个单元格。 以下是实际发生的情况: 2014-08-15 12:39:19.909 Pop
我正在开发一个应用程序,我有一个 UITableView,它使用 tableView 方法填充了一个 NSMutableArray:cellForRowAtIndexPath:,它工作正常,但问题是我
我在两个 View 之间沟通时遇到了麻烦。我的目标是向我的 NSMutableArray 添加一个新对象.感谢 stackoverflow 上可爱的人,他们已经修复了。现在我有另一个问题。 即使我能够
我有一个奇怪的错误。我有一个带有静态单元格的分组 UITableView,我想在每个部分下添加一个白色阴影。我考虑的方法是为一个部分中的每个最低单元格添加阴影。 所以在 viewDidAppear 中
每当我滚动我的 tableview 时,它都非常滞后。我认为这与我加载细胞的方式有关。我尽可能使用 UINib (5.0+),同时仍然提供向后兼容性。然后我从 NSArray 中的 NSDiction
我不明白cellForRowAtIndexPath函数的indexPath参数始终为0。我的表当前有7行。我的表格显示的是表格第一行的7倍。是什么导致indexPath始终为零? @implement
我正在开发一个使用表格 View 的 ToDoList iOS 应用程序。单元格内容(UITableCellView)由 nsmutablearray 的实例变量填充。 当我添加一个新的 ToDoIt
所以我有快速的代码,可以创建一个配置文件列表,并允许个人对其进行过滤和排序以及单击它们。就像地址簿一样。它以主/详细信息格式设置,其中主信息是人员列表,详细信息是个人。我还有一个来自 master 的
完整代码如下: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *
我有两个实体:日记帐和帐户。这种关系是一对多的:Journal 只有一个 Account。账户有很多日记账。我想根据每个帐户的日记显示总计,但我不知道如何操作。目前我为每个帐户获得的总额是相同的: 在
我的 cellForRowAtIndexPath 并非每次都会触发我将 dataSource 和 delegate 设置给文件所有者。我还设置了引用 socket 。我还尝试了 tbl.delegat
我的项目: 我的项目中有一个 UITableView。每个 UITableViewCell 都有一个 UITextView 作为 subview 。当用户点击“添加”按钮时,我需要再添加一个带有 UI
我的单元格中的数据在 cellforrowatindexpath 中混淆了。我更改了标签的文本颜色和文本,但即使 isComplete 值更改,单元格颜色和文本也会随机更改。 例如,当我滚动表格 Vi
免责声明:我使用的是 iOS 8,因此这可能是一个错误。 我正在尝试在特定事件发生后以编程方式编辑 UITableView 中特定 IndexPath 处单元格的背景颜色。我使用以下代码: UITab
填充 TableView 时出现奇怪的错误。这是我做过无数次的事情,但现在我对一些可能非常简单的事情感到困惑。也许我只是醒得太久了。 我有一个名为 riverGauge 的对象,它负责从 Web 服务
我的应用程序中有一个 tableView,当用户点击该单元格时,我需要它转到另一个 View Controller ,该 Controller 提供有关他们单击的单元格的更多详细信息。当我尝试使用 获
我是一名优秀的程序员,十分优秀!