我有一个UITableView
,里面有10个单元格。我有UIButton
&所有10个单元格内UILabel
。现在,如果我在第二个单元格中选择UIButton
,我将导航到另一个页面并选择一些值(字符串值)。现在,我希望将该字符串放在第二个单元格的UILabel
中。同样,如果我选择第5个单元格上的UIButton
,我将导航到另一个页面,选择一些字符串,并且该字符串必须放在第5个单元格的UILabel
中。如何在特定单元格的UILabel
内插入值。
赞赏我们的想法。
答案 0 :(得分:4)
我建议将第二个视图控制器设置为包含第一个视图控制器的实例,这样您就可以从第二个视图中操作第一个视图控制器的数据数组。
例如,在您创建FirstViewController
以执行推送的SecondViewController
部分中(因为正如您在评论中所说的那样,您正在使用{推送视图控制器} {1}}),同时使UINavigationController
包含SecondViewController
的实例。
FirstViewController
.m:
FirstViewController
在// Method to handle the push transition
- (void)buttonPressed:(UIButton*)button {
// Set the view controller's `selectedRow` property
// to contain the selected row number as contained in
// the button tag in this particular scenario
self.selectedRow = (int)button.tag;
// Create an instance of SecondViewController
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
// Pass the view controller along by setting
// the secondViewController's firstViewController
// property to contain self, i.e. the current
// instance of the FirstViewController
secondViewController.firstViewController = self;
// Push to the SecondViewController
[self.navigationController pushViewController:secondViewController animated:YES];
}
的.h中,添加SecondViewController
属性,以便可以从firstViewController
公开访问。
FirstViewController
.h:
SecondViewController
然后在#import "FirstViewController.h"
@interface SecondViewController : UIViewController
@property (weak, nonatomic) FirstViewController *firstViewController;
的.m中,您可以根据需要在SecondViewController
中操作作为表格数据源的数组,然后再弹出视图。
FirstViewController
.m:
SecondViewController
此外,您希望// Method called after the string is selected
- (void)stringSelected:(NSString*)string {
// Update the array to contain the string
// (or do whatever manipulation to whatever data
// structure is fitting in your particular case)
[self.firstViewController.array replaceObjectAtIndex:self.firstViewController.selectedRow withObject:string];
// To go back to the previous view, pop the view controller
[self.navigationController popViewControllerAnimated:YES];
}
的数据源UITableView
和array
属性包含在selectedRow
的.h中,以便它可以从FirstViewController
公开访问。
SecondViewController
.h:
FirstViewController
在@property (weak, nonatomic) NSMutableArray *array;
@property (nonatomic) int selectedRow;
的.m中,您希望确保在从其他视图返回时重新加载表数据,以便FirstViewController
反映更新的数据。
UITableView
.m:
FirstViewController
答案 1 :(得分:1)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellName" forIndexPath:indexPath];
if(cell == nil)
{
cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellName"];
}
//Assigning the contents of cell
cell.labelName.text = [NSString stringWithFormat:@"%@", [self.arrCategoryTitle objectAtIndex:indexPath.row]];
[cell.buttonName addTarget:self action:@selector(buttonNamePressed:) forControlEvents:UIControlEventTouchDown];
// Configure the cell...
return cell;
}
- (void)buttonNamePressed:(UIButton *)sender
{
CGPoint swipepoint = sender.center;
CGPoint rootViewPoint = [sender.superview convertPoint:swipepoint toView:self.tableName];
NSIndexPath *indexPath = [self.tableName indexPathForRowAtPoint:rootViewPoint];
NSLog(@"%@", indexPath);
[self.tableName cellForRowAtIndexPath:indexPath];
self.defaultRowId = indexPath.row;
[self performSegueWithIdentifier:@"viewControllerIdentifierName" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
DetailsViewController *detailsViewController = (DetailsViewController *)[(UINavigationController *)segue.destinationViewController topViewController];
detailsViewController.defaultrow = self.defaultRowId;
}
现在当你导航到secondViewController时,将选择字符串值设置为firstViewController的属性,然后重新加载表。