ios - 如何根据 headerLabel 标记 UITableView 部分上的按钮?
<p><p>我希望每个删除按钮都使用其关联部分的 <code>headerLabel.text</code> 进行标记。 </p>
<p>这样,当按下删除按钮运行 <code>deleteButtonPressed</code> 方法时,<code>deleteFromMatchCenter</code> Parse 函数将使用节的 <code>headerLabel.text</code> 值作为参数。我尝试按如下方式进行操作,但这似乎无法正确识别标题标题。 </p>
<p>如何正确地将每个删除按钮与其各自的部分标题标题相关联,并将其作为参数发送?</p>
<p><strong>MatchCenterViewController.m:</strong></p>
<pre><code>#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 = ;
if (self) {
}
return self;
}
- (void)viewDidLoad
{
;
self.matchCenter = [ 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;
;
_matchCenterArray = [ init];
}
- (void)viewDidAppear:(BOOL)animated
{
self.matchCenterArray = [ init];
[PFCloud callFunctionInBackground:@"MatchCenter"
withParameters:@{
@"test": @"Hi",
}
block:^(NSArray *result, NSError *error) {
if (!error) {
_matchCenterArray = result;
;
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 = [ initWithFrame:CGRectMake(0, 0, 320, 21)];
headerView.backgroundColor = ;
_searchTerm = [[[ objectForKey:@"Top 3"] objectAtIndex:3]objectForKey:@"Search Term"];
UILabel *headerLabel = [ initWithFrame:CGRectMake(8, 0, 250, 21)];
headerLabel.text = ;
headerLabel.font = ];
headerLabel.textColor = ;
headerLabel.backgroundColor = ;
;
UIButton *deleteButton = ;
deleteButton.tag = section;
deleteButton.frame = CGRectMake(300, 2, 17, 17);
forState:UIControlStateNormal];
;
;
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 = ;
if (!cell) {
// if no cell could be dequeued create a new one
cell = [ initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// No cell seperators = clean design
tableView.separatorColor = ;
// title of the item
cell.textLabel.text = _matchCenterArray[@"Top 3"][@"Title"];
cell.textLabel.font = ;
// price of the item
cell.detailTextLabel.text = [@"Top 3"][@"Price"]];
cell.detailTextLabel.textColor = ;
// image of the item
NSData *imageData = [@"Top 3"][@"Image URL"]]];
[ setImage:];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 65;
}
- (void)deleteButtonPressed:(id)sender
{
// Define the sections title
NSString *sectionName = ;
// Run delete function with respective section header as parameter
[PFCloud callFunctionInBackground:@"deleteFromMatchCenter"
withParameters:
@{@"searchTerm": sectionName,}
block:^(NSDictionary *result, NSError *error) {
if (!error) {
NSLog(@"Result: '%@'", result);
;
}
}];
}
- (void)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 .
// Pass the selected object to the new view controller.
}
*/
@end
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您尚未链接您的 <code>titleForHeaderInSection:</code> 方法,但是当您无权访问 <code>indexPath</code> 时,您正在尝试传递 indexPath.section。</code> p>
<p>试试这个</p>
<pre><code>UIButton *deleteButton = (UIButton *)sender;
NSString *sectionName = ;
</code></pre>
<p>或</p>
<pre><code>NSString *sectionName = _searchTerm = [[[ objectForKey:@"Top 3"] objectAtIndex:3]objectForKey:@"Search Term"];
</code></pre></p>
<p style="font-size: 20px;">关于ios - 如何根据 headerLabel 标记 UITableView 部分上的按钮?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/24472327/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/24472327/
</a>
</p>
页:
[1]