Iphone UtableView与Xcode中的gridview类似?
我正在实现UITableView的概念,就像gridView一样。我正在UITableView中加载NSMutableArrays数据。我正在加载tableViewCell的UIButtons中的数组项,当按下一个按钮时,应该执行一些操作。即每行有4个按钮,行数取决于数组项数。我可以部分执行。我可以单击按钮,只要数组中有8个或少于8个项目,按钮的下一个操作就会执行。但是,如果超过8个项目,则我可以查看添加的按钮,但我无法单击按钮(即-(iAction)按钮按下:(id)发件人{)未响应..无法理解我的错误所在Iphone UtableView与Xcode中的gridview类似?,iphone,xcode,uitableview,Iphone,Xcode,Uitableview,我正在实现UITableView的概念,就像gridView一样。我正在UITableView中加载NSMutableArrays数据。我正在加载tableViewCell的UIButtons中的数组项,当按下一个按钮时,应该执行一些操作。即每行有4个按钮,行数取决于数组项数。我可以部分执行。我可以单击按钮,只要数组中有8个或少于8个项目,按钮的下一个操作就会执行。但是,如果超过8个项目,则我可以查看添加的按钮,但我无法单击按钮(即-(iAction)按钮按下:(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;
![如何单击所有按钮并响应-(iAction)][1]您可以尝试
还有一个git示例:
希望它能对您有所帮助。如果您的目标是iOS 6,那么总是会有UICollectionView。如果没有,您可以创建UILabels并将其添加到tableview中,并利用您的创造力将其显示为网格视图。Hi希望您得到答案,如果没有,则执行以下代码:
- (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;
}
-(CGFloat)tableView:(UITableView*)tableView rowatinexpath的高度:(nsindepath*)indepath
{
int numRows=0;
NSMutableArray*sectionItems=[sections-objectAtIndex:indexPath.section];
如果([sectionItems count]使用自定义单元格,单元格内根据您的要求使用2或3个标签。good quetion@arizah..这里使用带有4个按钮的自定义单元格,并为其创建委托方法,并使用标记处理它。如果此数据是静态的,我的意思是所有日期的格式相同,则执行此逻辑匹配:)+1代表que..为什么不选择ScrollView…我在-(UITableViewCell*)tableView:(UITableView*)tableView CellForRowatineXpath:(NSIndexPath*)indexPath{中使用了自定义单元格na。实际上,我需要执行一些操作,例如在选择相应时间时打开警报框。因此选择了UIButtons..bcoz UIButtons有-(iAction) event@Paras你能详细解释一下吗我听不懂。。?
- (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;
}