我正在实现 UITableView 的概念,就像 gridView 一样。我在 UITableView 中加载 NSMutableArrays 数据。我在 tableViewCell 的 UIButtons 中加载数组项,当按下按钮时,应该执行一些操作..即每行 4 个按钮和行数取决于数组项的数量。我可以部分完成。我可以单击按钮,只要数组中有 8 个或少于 8 个项,按钮的下一步操作就会进行。但是当有更多项时然后我可以查看添加的按钮,但我无法单击按钮(即 -(IBAction)buttonPressed:(id)sender{ )没有响应..无法理解我哪里出错了..?
sections=[[NSMutableArray alloc] init];
for(int s=0;s<1;s++)
{
NSMutableArray *section=[[NSMutableArray alloc] init];
for(int i=0;i<[arr1 count];i++)
{
Item *item=[[Item alloc] init];
NSString *eventName=[[arr1 objectAtIndex:i]objectForKey:@"Time"];
item.Time=eventName;
[section addObject:item];
}
[sections addObject:section];
}
tableView=[[UITableView alloc]initWithFrame:CGRectMake(0,430,320,200) style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableArray *sectionItems=[sections objectAtIndex:indexPath.section];
int numRows=[sectionItems count]/[arr1 count];
return numRows * 80.0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *hlCellID=@"hlCellID";
UITableViewCell* hlcell=[tableView dequeueReusableCellWithIdentifier:hlCellID];
if(hlcell == nil)
{
hlcell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:hlCellID]autorelease];
hlcell.accessoryType = UITableViewCellAccessoryNone;
hlcell.selectionStyle = UITableViewCellSelectionStyleNone;
}
int section=indexPath.section;
NSMutableArray *sectionItems=[sections objectAtIndex:section];
int n=[sectionItems count];
int i=0,i1=0;
while(i<n)
{
int yy= 4+i1*34;
int j=0;
for(j=0;j<4;j++)
{
if(i>=n)break;
Item *item=[sectionItems objectAtIndex:i];
CGRect rect=CGRectMake(0+70*j,yy,79,40);
UIButton *button=[[UIButton alloc] initWithFrame:rect];
[button setFrame:rect];
[button setContentMode:UIViewContentModeLeft];
button.titleLabel.font = [UIFont systemFontOfSize:14];
NSString *settitle=[NSString stringWithFormat:@"%@",item.Time];
[button setTitle:settitle forState:UIControlStateNormal];
NSString *tagValue=[NSString stringWithFormat:@"%d%d",indexPath.section+1,i];
button.tag=[tagValue intValue];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setShowsTouchWhenHighlighted:YES];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[hlcell.contentView addSubview:button];
[button release];
i++;
}
i1=i1+1;
}
return hlcell;
}
-(IBAction)buttonPressed:(id)sender
{
int tagID=[sender tag];
int divnum=0;
if(tagID<100)
divnum=10;
else
divnum=100;
int section=[sender tag]/divnum;
section-=1;
int itemId=[sender tag]%divnum;
NSMutableArray *sectionItems=[sections objectAtIndex:section];
Item *item=[sectionItems objectAtIndex:itemId];
}
In Item class I have NSString *Time;
我的数组是这样的:
(
{
Time = "9:00 am";
},
{
Time = "9:15 am";
},
{
Time = "9:30 am";
},
{
Time = "9:45 am";
},
{
TimeStart = "10:00 am";
},
{
Time = "10:15 am";
},
......24 objects in the array
![如何让所有按钮都被点击并响应 -(IBAction)][1]