【发布时间】:2016-10-15 23:43:27
【问题描述】:
我在UIViewController 中创建了表格视图,并成功创建了单元格,但是当按下任何单元格时我不知道该怎么做。我想如果按一个单元格,转到另一个视图控制器。我添加了一些方法,但没有奏效。我应该添加哪些方法以及如何将方法与操作联系起来?
此方法用于创建数组
-(void)addCell{
int wholeNumber = 500;
int leftOver = wholeNumber%90;
int sectionNumber = wholeNumber / 90;;
tableTitle = [[NSMutableArray alloc] init];
tableSubtitle = [[NSMutableArray alloc] init];
for (int i =1; i<= sectionNumber; i++) {
if (i == 1) {
NSString *stringForTitle = [NSString stringWithFormat:@"%d",i];
NSString *stringForsubitle = [NSString stringWithFormat:@"90 Questions. Free."];
[tableTitle addObject:stringForTitle];
[tableSubtitle addObject:stringForsubitle];
}else if (i==2){
NSString *stringForTitle = [NSString stringWithFormat:@"%d",i];
NSString *stringForsubitle = [NSString stringWithFormat:@"90 Questions. Unlock or Rate App."];
[tableTitle addObject:stringForTitle];
[tableSubtitle addObject:stringForsubitle];
}else if (i==3){
NSString *stringForTitle = [NSString stringWithFormat:@"%d",i];
NSString *stringForsubitle = [NSString stringWithFormat:@"90 Questions. Unlock or Sign in and rate 15 questions."];
[tableTitle addObject:stringForTitle];
[tableSubtitle addObject:stringForsubitle];
}else if (i==4){
NSString *stringForTitle = [NSString stringWithFormat:@"%d",i];
NSString *stringForsubitle = [NSString stringWithFormat:@"90 Questions. Unlock or Sign in and write explanation for 4 questions."];
[tableTitle addObject:stringForTitle];
[tableSubtitle addObject:stringForsubitle];
}else if (i>4){
NSString *stringForTitle = [NSString stringWithFormat:@"%d",i];
NSString *stringForsubitle = [NSString stringWithFormat:@"90 Questions. Unlock or Sign in and contribute 2 questions."];
[tableTitle addObject:stringForTitle];
[tableSubtitle addObject:stringForsubitle];
}else{
}
}
if (leftOver == 0) {
} else {
if (sectionNumber == 0) {
NSString *stringForTitle = [NSString stringWithFormat:@"1"];
NSString *stringForsubitle = [NSString stringWithFormat:@"%d Questions. Free.",leftOver];
[tableTitle addObject:stringForTitle];
[tableSubtitle addObject:stringForsubitle];
}else if (sectionNumber==1){
NSString *stringForTitle = [NSString stringWithFormat:@"2"];
NSString *stringForsubitle = [NSString stringWithFormat:@"%d Questions. Unlock or Rate app.",leftOver];
[tableTitle addObject:stringForTitle];
[tableSubtitle addObject:stringForsubitle];
}else if (sectionNumber==2){
NSString *stringForTitle = [NSString stringWithFormat:@"3"];
NSString *stringForsubitle = [NSString stringWithFormat:@"%d Questions. Unlock or Sign in and rate 15 questions.",leftOver];
[tableTitle addObject:stringForTitle];
[tableSubtitle addObject:stringForsubitle];
}else if (sectionNumber==3){
NSString *stringForTitle = [NSString stringWithFormat:@"4"];
NSString *stringForsubitle = [NSString stringWithFormat:@"%d Questions. Unlock or Sign in and write explanation for 3 questions.",leftOver];
[tableTitle addObject:stringForTitle];
[tableSubtitle addObject:stringForsubitle];
}else if (sectionNumber>3){
NSString *stringForTitle = [NSString stringWithFormat:@"%d",sectionNumber +1];
NSString *stringForsubitle = [NSString stringWithFormat:@"%d Questions. Unlock or Sign in and contribute 1 questions.",leftOver];
[tableTitle addObject:stringForTitle];
[tableSubtitle addObject:stringForsubitle];
}else {
}
}
NSString *stringForTitle = [NSString stringWithFormat:@"CQ"];
NSString *stringForsubitle = [NSString stringWithFormat:@"Contribute Questions."];
[tableTitle addObject:stringForTitle];
[tableSubtitle addObject:stringForsubitle];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableTitle count];
}
此方法创建单元格
-(UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"thisCell"];
UILabel *titleLable= [[UILabel alloc] initWithFrame:CGRectMake(0, 4, 60, 40)];
titleLable.text = [tableTitle objectAtIndex:indexPath.row];
[titleLable setFont:[UIFont systemFontOfSize:25]];
titleLable.backgroundColor = [UIColor whiteColor];
titleLable.textAlignment = NSTextAlignmentCenter;
CALayer* layer = [titleLable layer];
CALayer *bottomBorder = [CALayer layer];
bottomBorder.borderColor = [UIColor lightGrayColor].CGColor;
bottomBorder.borderWidth = 1;
bottomBorder.frame = CGRectMake(-1, layer.frame.size.height-1, layer.frame.size.width, 1);
[bottomBorder setBorderColor:[UIColor lightGrayColor].CGColor];
[layer addSublayer:bottomBorder];
[cell.contentView addSubview:titleLable];
CGFloat width = [UIScreen mainScreen].bounds.size.width;
CGFloat actualwidth = width - 100;
UILabel *subtitleLable = [[UILabel alloc] initWithFrame:CGRectMake(60, 4, actualwidth, 40)];
subtitleLable.text = [tableSubtitle objectAtIndex:indexPath.row];
subtitleLable.numberOfLines = 0;
[subtitleLable setFont:[UIFont systemFontOfSize:10]];
subtitleLable.backgroundColor = [UIColor whiteColor];
CALayer* layer2 = [subtitleLable layer];
CALayer *bottomBorder2 = [CALayer layer];
bottomBorder2.borderColor = [UIColor lightGrayColor].CGColor;
bottomBorder2.borderWidth = 1;
bottomBorder2.frame = CGRectMake(-1, layer2.frame.size.height-1, layer2.frame.size.width, 1);
[bottomBorder2 setBorderColor:[UIColor lightGrayColor].CGColor];
[layer2 addSublayer:bottomBorder2];
[cell.contentView addSubview:subtitleLable];
UILabel *spaceLable = [[UILabel alloc] initWithFrame:CGRectMake(actualwidth+60, 4, 100, 40)];
spaceLable.text = @"";
spaceLable.numberOfLines = 0;
[spaceLable setFont:[UIFont systemFontOfSize:11]];
spaceLable.backgroundColor = [UIColor whiteColor];
CALayer* layer3 = [spaceLable layer];
CALayer *bottomBorder3 = [CALayer layer];
bottomBorder3.borderColor = [UIColor lightGrayColor].CGColor;
bottomBorder3.borderWidth = 1;
bottomBorder3.frame = CGRectMake(-1, layer3.frame.size.height-1, layer3.frame.size.width, 1);
[bottomBorder3 setBorderColor:[UIColor lightGrayColor].CGColor];
[layer3 addSublayer:bottomBorder3];
[cell.contentView addSubview:spaceLable];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
rowNo = indexPath.row;
}
这是表格视图的图像
【问题讨论】:
标签: ios objective-c xcode uitableview