我实现UITableView的概念,就像gridView一样。我在UITableView中加载NSMutableArrays数据。我在tableViewCell的UIButtons中加载数组项目,当按下按钮时,应该执行一些操作.. 4个按钮在每行和没有行取决于数组项目的数量。我可以部分地完成它。我可以点击按钮,只要数组中有8个或少于8个项目,按钮的下一个动作就会出现。但是当有数据时不仅如此,我可以查看添加的按钮,但我无法点击按钮(即 - (IBAction)buttonPressed:(id)发送者{)没有响应..不明白我错在哪里..?
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]
答案 0 :(得分:1)
答案 1 :(得分:0)
答案 2 :(得分:0)
您可以创建UILabel并将它们添加到您的tableview中并使用您的创造力,使其像网格视图一样显示。
答案 3 :(得分:0)
您希望得到您的答案,如果没有,那么请遵守以下代码:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
int numRows=0;
NSMutableArray *sectionItems = [sections objectAtIndex:indexPath.section];
if ([sectionItems count]<5)
{
numRows = [sectionItems count]/1;
numRows =numRows *152;
NSLog(@"result less then 6 or zero");
}
else
{
numRows = [sectionItems count]/6;
numRows =numRows *152.0;
}
//return numRows * 152.0; //145
return numRows;
}
问题是,当我们计算按钮的高度时。 numRows = [sectionItems count] / 1;因此,根据您的需要管理这条线......
希望这对你有用......