Ios 如何在UITableView中将节与其标题标签关联
我的Ios 如何在UITableView中将节与其标题标签关联,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我的UITableView中的每个部分都有一个headerLabel.text值,我想让它在deleteButtonPressediAction内部运行的deleteFromMatchCenter函数中使用该值作为参数 正如你在我的代码中看到的那样,我尝试过这样做,但正如预期的那样,应用程序无法编译,因为headerLabel.text不是公共属性。如何使节上的“删除”按钮使用其特定的标题标签作为参数 MatchCenterViewController.h: #import <UIKit
UITableView
中的每个部分都有一个headerLabel.text
值,我想让它在deleteButtonPressed
iAction内部运行的deleteFromMatchCenter
函数中使用该值作为参数
正如你在我的代码中看到的那样,我尝试过这样做,但正如预期的那样,应用程序无法编译,因为headerLabel.text
不是公共属性。如何使节上的“删除”按钮使用其特定的标题标签作为参数
MatchCenterViewController.h:
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
#import "AsyncImageView.h"
#import "SearchViewController.h"
@interface MatchCenterViewController : UIViewController <UITableViewDataSource>
@property (nonatomic) IBOutlet NSString *itemSearch;
@property (nonatomic, strong) NSArray *imageURLs;
@property (strong, nonatomic) NSString *matchingCategoryCondition;
@property (strong, nonatomic) NSString *matchingCategoryLocation;
@property (strong, nonatomic) NSNumber *matchingCategoryMaxPrice;
@property (strong, nonatomic) NSNumber *matchingCategoryMinPrice;
@property (strong, nonatomic) NSArray *matchCenterArray;
@property (strong, nonatomic) NSString *searchTerm;
@end
MatchCenterViewController.h:
#进口
#进口
#导入“AsyncImageView.h”
#导入“SearchViewController.h”
@接口匹配CenterViewController:UIViewController
@属性(非原子)IBMString*itemSearch;
@属性(非原子,强)NSArray*imageURL;
@属性(强,非原子)NSString*匹配类别条件;
@属性(强,非原子)NSString*匹配类别位置;
@属性(强,非原子)NSNumber*匹配类别映射;
@属性(强,非原子)NSNumber*匹配类别价格;
@属性(强,非原子)NSArray*matchCenterArray;
@属性(强,非原子)NSString*searchTerm;
@结束
MatchCenterViewController.m:
#import "MatchCenterViewController.h"
#import <UIKit/UIKit.h>
@interface MatchCenterViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *matchCenter;
@end
@implementation MatchCenterViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.matchCenter = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewCellStyleSubtitle];
self.matchCenter.frame = CGRectMake(0,50,320,self.view.frame.size.height-100);
_matchCenter.dataSource = self;
_matchCenter.delegate = self;
[self.view addSubview:self.matchCenter];
_matchCenterArray = [[NSArray alloc] init];
}
- (void)viewDidAppear:(BOOL)animated
{
self.matchCenterArray = [[NSArray alloc] init];
[PFCloud callFunctionInBackground:@"MatchCenter"
withParameters:@{
@"test": @"Hi",
}
block:^(NSArray *result, NSError *error) {
if (!error) {
_matchCenterArray = result;
[_matchCenter reloadData];
NSLog(@"Result: '%@'", result);
}
}];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return _matchCenterArray.count;
}
//the part where i setup sections and the deleting of said sections
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 21.0f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 21)];
headerView.backgroundColor = [UIColor lightGrayColor];
_searchTerm = [[[[_matchCenterArray objectAtIndex:section] objectForKey:@"Top 3"] objectAtIndex:3]objectForKey:@"Search Term"];
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, 0, 250, 21)];
headerLabel.text = [NSString stringWithFormat:@"%@", _searchTerm];
headerLabel.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
headerLabel.textColor = [UIColor whiteColor];
headerLabel.backgroundColor = [UIColor lightGrayColor];
[headerView addSubview:headerLabel];
UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
deleteButton.tag = section + 1000;
deleteButton.frame = CGRectMake(300, 2, 17, 17);
[deleteButton setImage:[UIImage imageNamed:@"xbutton.png"] forState:UIControlStateNormal];
[deleteButton addTarget:self action:@selector(deleteButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[headerView addSubview:deleteButton];
return headerView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Initialize cell
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
// if no cell could be dequeued create a new one
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// No cell seperators = clean design
tableView.separatorColor = [UIColor clearColor];
// title of the item
cell.textLabel.text = _matchCenterArray[indexPath.section][@"Top 3"][indexPath.row][@"Title"];
cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
// price of the item
cell.detailTextLabel.text = [NSString stringWithFormat:@"$%@", _matchCenterArray[indexPath.section][@"Top 3"][indexPath.row][@"Price"]];
cell.detailTextLabel.textColor = [UIColor colorWithRed:0/255.0f green:127/255.0f blue:31/255.0f alpha:1.0f];
// image of the item
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:_matchCenterArray[indexPath.section][@"Top 3"][indexPath.row][@"Image URL"]]];
[[cell imageView] setImage:[UIImage imageWithData:imageData]];
//[cell imageView].frame = CGRectMake(45.0,10.0,10,10);
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 65;
}
- (IBAction)deleteButtonPressed:(UIButton *)sender {
[PFCloud callFunctionInBackground:@"deleteFromMatchCenter"
withParameters:@{
@"searchTerm": headerLabel.text,
}
block:^(NSDictionary *result, NSError *error) {
if (!error) {
NSLog(@"Result: '%@'", result);
[_matchCenter reloadData];
}
}];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
#导入“MatchCenterViewController.h”
#进口
@接口匹配CenterViewController()
@属性(非原子,强)UITableView*matchCenter;
@结束
@CenterViewController的实现
-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
如果(自我){
}
回归自我;
}
-(无效)viewDidLoad
{
[超级视图下载];
self.matchCenter=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewCellStyleSubtitle];
self.matchCenter.frame=CGRectMake(0,50320,self.view.frame.size.height-100);
_matchCenter.dataSource=self;
_matchCenter.delegate=self;
[self.view addSubview:self.matchCenter];
_matchCenterArray=[[NSArray alloc]init];
}
-(无效)视图显示:(BOOL)动画
{
self.matchCenterArray=[[NSArray alloc]init];
[PFCloud callFunctionInBackground:@“MatchCenter”
带参数:@{
@“测试”:@“嗨”,
}
块:^(NSArray*结果,NSError*错误){
如果(!错误){
_matchCenterArray=结果;
[_matchCenter重新加载数据];
NSLog(@“结果:'%@'”,结果);
}
}];
}
-(NSInteger)表格视图中的节数:(UITableView*)表格视图
{
返回_matchCenterArray.count;
}
//我设置节和删除所述节的部分
-(CGFloat)表格视图:(UITableView*)表格视图头部高度部分:(NSInteger)部分{
返回21.0f;
}
-(UIView*)表格视图:(UITableView*)表格视图用于标题部分:(NSInteger)部分{
UIView*headerView=[[UIView alloc]initWithFrame:CGRectMake(0,0320,21)];
headerView.backgroundColor=[UIColor lightGrayColor];
_searchTerm=[[[u matchCenterArray objectAtIndex:section]objectForKey:@“前三名”]objectAtIndex:3]objectForKey:@“搜索术语”];
UILabel*headerLabel=[[UILabel alloc]initWithFrame:CGRectMake(8,0,250,21)];
headerLabel.text=[NSString stringWithFormat:@“%@”,_searchTerm];
headerLabel.font=[UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
headerLabel.textColor=[UIColor whiteColor];
headerLabel.backgroundColor=[UIColor lightGrayColor];
[headerView添加子视图:headerLabel];
UIButton*deleteButton=[UIButton按钮类型:UIButtonTypeCustom];
deleteButton.tag=截面+1000;
deleteButton.frame=CGRectMake(300,2,17,17);
[deleteButton setImage:[UIImage ImageName:@“xbutton.png”]用于状态:UIControlStateNormal];
[deleteButton addTarget:self action:@selector(deleteButton按下:)for ControlEvents:UIControlEventTouchUpInside];
[headerView addSubview:deleteButton];
返回headerView;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
返回3;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
//初始化单元格
静态NSString*CellIdentifier=@“Cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(!单元格){
//如果没有单元格可以退出队列,请创建一个新的单元格
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle重用标识符:CellIdentifier];
}
//无单元分离器=清洁设计
tableView.separatorColor=[UIColor clearColor];
//项目名称
cell.textlab.text=_matchCenterArray[indexPath.section][@“Top 3”][indexPath.row][@“Title”];
cell.textlab.font=[UIFont boldSystemFontOfSize:14];
//商品价格
cell.detailTextLabel.text=[NSString stringWithFormat:@“$%@”,_matchCenterArray[indexath.section][@“Top 3”][indexPath.row][@“Price”];
cell.detailtextlab.textColor=[UIColor colorWithRed:0/255.0f绿色:127/255.0f蓝色:31/255.0f alpha:1.0f];
//项目的图像
NSData*imageData=[NSData DATA WITHCONTENTSOFURL:[NSURL URLWithString:_matchCenterArray[indexPath.section][@“Top 3”][indexPath.row][@“图像URL”]];
[[cell imageView]设置图像:[UIImage imageWithData:imageData]];
//[cell imageView].frame=CGRectMake(45.0,10.0,10,10);
返回单元;
}
-(CGFloat)tableView:(UITableView*)表视图行高度索引路径:(NSIndexPath*)索引路径
{
返回65;
}
-(iAction)DeleteButton按下:(UIButton*)发送方{
[PFCloud callFunctionInBackground:@“deleteFromMatchCenter”
带参数:@
- (NSString *)textForSection:(NSInteger)section {
[[[[_matchCenterArray objectAtIndex:section] objectForKey:@"Top 3"] objectAtIndex:section] objectForKey:@"Search Term"];
}
- (IBAction)deleteButtonPressed:(UIButton *)sender {
NSString *sectionText = [self textForSection:(sender.tag - 1000)];
[PFCloud callFunctionInBackground:@"deleteFromMatchCenter"
withParameters:@{
@"searchTerm": sectionText,
}
block:^(NSDictionary *result, NSError *error) {
if (!error) {
NSLog(@"Result: '%@'", result);
[_matchCenter reloadData];
}
}];
}
UIButton *deleteButton = [UIButton buttonWithAction:^{
[self deleteButtonPressedForSection:section];
}];
- (void)deleteButtonPressedForSection:(NSInteger)section {
NSString *sectionText = [self textForSection:section];
}
+ (instancetype)buttonWithAction:(CompletionBlock)action {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:action action:@selector(invoke) forControlEvents:UIControlEventTouchUpInside];
[button setAssociatedObject:action forSelector:_cmd]; //you're going to have to write this method yourself
return button;
}