我有一个UITableView
,其中包含一定数量的部分和行。
当用户选择单元格时,如何在My ViewController中执行加号和减号按钮的操作?我的意思是分别增加每行的标签值?
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
NSArray *toBeReturned = [NSArray arrayWithArray:
[@"A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z"
componentsSeparatedByString:@"|"]];
return toBeReturned;
}
//行的单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier= @"MyIdentifier";
LocationsCustomCell *cell= (LocationsCustomCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if(cell==nil)
{
[[NSBundle mainBundle] loadNibNamed:@"LocationsCustomCell" owner:self options:nil];
cell=locationCustomcell;
}
@try
{
if(indexPath.section<=[arrayOfCharacters count])
{
NSString *sortedStr =[[objectsForCharacters objectForKey:[arrayOfCharacters objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
if([sortedStr isKindOfClass:[NSNull class]])
{
cell.locationNameLabel.text=@"";
}
else
{
cell.locationNameLabel.text=sortedStr;
cell.locationNameLabel.textColor=[UIColor blackColor];
}
}
}
@catch (NSException *exception)
{
}
sectionNo=indexPath.section;
cell.plusButn.tag = sectionNo*100+indexPath.row;
cell.minusButn.tag = sectionNo*100+indexPath.row;
cell.locationCountLabel.tag=sectionNo*100+indexPath.row;
plusElement=[indexPath row];
// NSInteger sectionmame=[indexPath section];
// NSInteger rowname =[indexPath row];
minusElement=[indexPath row];
countLabelElement=[indexPath row];
[cell.plusButn addTarget:self action:@selector(plusButnPressed:) forControlEvents:UIControlEventValueChanged];
//[cell.plusButn addTarget: self
// action: @selector(buttonPressed:withEvent:)
//forControlEvents: UIControlEventTouchUpInside];
// [cell.minusButn addTarget:self action:@selector(minusButnPressed:) forControlEvents:UIControlEventValueChanged];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
return cell;
}
-(IBAction)plusButnPressed:(id)sender
{
NSLog(@"selectedSection Number/plusButn is--%d",sectionNumber);
LocationsCustomCell *cell=(LocationsCustomCell *)[self.locationsTableVw cellForRowAtIndexPath:[NSIndexPath indexPathForRow:plusElement inSection:sectionNumber]];
cell.plusButn=(UIButton *)sender;
if (cell.plusButn)
{
Countvalue++;
NSString *ValueStr= [NSString stringWithFormat:@"%d",Countvalue];
cell.locationCountLabel.text=ValueStr;
}
}
-(IBAction)minusButnPressed:(id)sender
{
NSLog(@"selectedSection Number/minusButn is--%d",sectionNumber);
LocationsCustomCell *cell=(LocationsCustomCell *)[self.locationsTableVw cellForRowAtIndexPath:[NSIndexPath indexPathForRow:minusElement inSection:sectionNumber]];
cell.minusButn=(UIButton *)sender;
if (cell.minusButn)
{
Countvalue--;
NSString *ValueStr= [NSString stringWithFormat:@"%d",Countvalue];
cell.locationCountLabel.text=ValueStr;
}
}
答案 0 :(得分:1)
试试吧
在CellForrowAtIndexPath中
[theCell.button addTarget: self
action: @selector(buttonPressed:withEvent:)
forControlEvents: UIControlEventTouchUpInside];
然后您的事件处理程序将如下所示:
- (void) buttonPressed: (id) sender withEvent: (UIEvent *) event
{
UITouch * touch = [[event touches] anyObject];
CGPoint location = [touch locationInView: self.tableView];
NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint: location];
/* indexPath contains the index of the row containing the button */
Nsstring *str=[self.tableView cellForRowAtIndexPath:indexPath].locationCountLabel.text;
int a=[str intValue];
a=a+1;
[self.tableView cellForRowAtIndexPath:indexPath].locationCountLabel.text=[NSString stringWithFormat:@"%d",a];
}
你可以用减号来完成它,同样只减少a的值。
修改强>
把这一行
[cell.plusButn addTarget:self action:@selector(plusButnPressed:) forControlEvents:UIControlEventValueChanged];
in if(cell == nil)像这样运行你的代码而不是我的代码。
if(cell==nil)
{
[[NSBundle mainBundle] loadNibNamed:@"LocationsCustomCell" owner:self options:nil];
cell=locationCustomcell;
[cell.plusButn addTarget:self action:@selector(plusButnPressed:) forControlEvents:UIControlEventTouchUpInside]; /////////this line is put here
}