我正在研究UITable视图并能够配置数据,每件事情都进展顺利。但我在配置方面遇到问题,我在下面解释我的想法。
a)我在我的项目中使用了2个viewcontrollers。
b)在第一个vew控制器上,我添加了2个按钮来显示相同的表视图(即第二个viewcontroller),但格式不同。
c)在第一个按钮上单击我想在我的单元格上显示公开按钮图标,在其他按钮的单击上我想删除公开按钮并仅显示删除图标。
我正在使用相同的表格视图来进行此过程。 关于这个过程,请帮助我。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ FirstViewController.h文件
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #import
@interface firstviewViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIButton *viewbtn;
- (IBAction)viewdata:(id)sender;
- (IBAction)deletedata:(id)sender;
@property (strong, nonatomic) IBOutlet UIButton *deletebtn;
@end
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ FirstViewController.m文件 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#import "firstviewViewController.h"
#import "tbViewController.h"
#import "customtableViewController.h"
@interface firstviewViewController ()
@end
@implementation firstviewViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)viewdata:(id)sender {
//tbViewController *tbv = [[tbViewController alloc] initWithNibName:@"tbViewController" bundle:nil];
customtableViewController *ct = [[customtableViewController alloc] initWithNibName:@"customtableViewController" bundle:nil];
[self.navigationController pushViewController:ct animated:YES];
}
- (IBAction)deletedata:(id)sender {
//tbViewController *tbv = [[tbViewController alloc] initWithNibName:@"tbViewController" bundle:nil];
customtableViewController *ct = [[customtableViewController alloc] initWithNibName:@"customtableViewController" bundle:nil];
[self.navigationController pushViewController:ct animated:YES];
}
@end
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ SecondViewController.h文件
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#import <UIKit/UIKit.h>
@interface tbViewController : UITableViewController<UITableViewDelegate, UITableViewDataSource>
{
NSMutableArray *tableData;
NSMutableArray *tableData1;
}
@property (strong, nonatomic) NSMutableArray *tableData;
@property (strong, nonatomic) NSMutableArray *tableData1;
@end
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ SecondViewController.m文件
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#import "tbViewController.h"
#import "firstviewViewController.h"
@interface tbViewController ()
@end
@implementation tbViewController
@synthesize tableData,tableData1;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
tableData = [NSMutableArray arrayWithObjects:@"Egg ", @"Mushroom ", @"Full Breakfast", @"Hamburger", @"Ham and Egg Sandwich", @"Creme Brelee", @"White Chocolate Donut", @"Starbucks Coffee", @"Vegetable Curry", nil];
tableData1 = [NSMutableArray arrayWithObjects: @"Instant Noodle with Egg", @"Noodle with BBQ Pork", @"Japanese Noodle with Pork", @"Green Tea", @"Thai Shrimp Cake", @"Angry Birds Cake", @"Ham and Cheese Panini", nil];
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStyleBordered target:self action:@selector(Edit:)];
[self.navigationItem setRightBarButtonItem:addButton];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (IBAction) Edit:(id)sender{
if(self.editing)
{
[super setEditing:NO animated:NO];
//[UITableView setEditing:NO animated:NO];
//[UITableView reloadData];
[self.navigationItem.leftBarButtonItem setTitle:@"Edit"];
[self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStylePlain];
}
else
{
[super setEditing:YES animated:YES];
//[UITableView setEditing:YES animated:YES];
//[UITableView reloadData];
[self.navigationItem.leftBarButtonItem setTitle:@"Done"];
[self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStyleDone];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return [tableData 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];
}
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:@"1.png" ];
cell.accessoryType = UITableViewCellEditingStyleInsert;
return cell;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
// [tableData removeObjectAtIndex:indexPath.row];
// [tableView reloadData];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
[tableData removeObjectAtIndex:indexPath.row];
[tableView reloadData];
}
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
NSString *item = [tableData objectAtIndex:fromIndexPath.row];
[tableData removeObject:item];
[tableData insertObject:item atIndex:toIndexPath.row];
}
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
@end