(iOS7)Table Viewの一部のCellの罫線(セパレート)を削除したい


Table View上の特定のCellの上下の罫線(セパレート)を削除したいのですが、
下記のコードでは全てのCellのセパレートが消えてしまいます。
特定のCellのみセパレートを削除する方法はないのでしょうか?

[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];

宜しくお願いします。

回答の条件
  • 1人5回まで
  • 登録:
  • 終了:2014/02/09 10:45:48
※ 有料アンケート・ポイント付き質問機能は2023年2月28日に終了しました。

ベストアンサー

id:bg5551 No.1

回答回数1184ベストアンサー獲得回数80

ポイント100pt

こちらの方法ではいかがでしょうか。

http://umeji.blogspot.jp/2010_09_01_archive.html

id:ankomoti3260

コメントありがとうございます。
ご紹介いただきました方法ですと、全ての境界線が削除されてしまいます。
質問内容がわかりにくくすみませんでした。
やりたかったのは、2セクションのうち2つ目のセクションのセルの境界線を削除したく、fiwaさんご紹介のコードから下記のコードで出来るようになりました。


  • (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return 2;
}

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

if( section == 0){
return 2;
}else{
return 1;
}
return 1;
}

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (!cell) { // yes
// セルを作成
cell = UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}

//cell.separatorInset = UIEdgeInsetsMake(0,10000,0,0);

if(indexPath.section == 0){
if (indexPath.row == 0) {
UIView *line = UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1)];
line.backgroundColor = [UIColor colorWithRed:0.82f green:0.82f blue:0.82f alpha:1.0f];
[cell addSubview:line];
//[cell bringSubviewToFront:line];
} else if (indexPath.row == 2 - 1) {
UIView *line = UIView alloc] initWithFrame:CGRectMake(21, 0, 320, 1)];
line.backgroundColor = [UIColor colorWithRed:0.82f green:0.82f blue:0.82f alpha:1.0f];
[cell addSubview:line];
//[cell bringSubviewToFront:line];

UIView *lineBottom = UIView alloc] initWithFrame:CGRectMake(0, 43, 320, 1)];
lineBottom.backgroundColor = [UIColor colorWithRed:0.82f green:0.82f blue:0.82f alpha:1.0f];
[cell addSubview:lineBottom];
//[cell bringSubviewToFront:lineBottom];
}
}

cell.textLabel.text = @"test";
return cell;
}


  • (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{
if(indexPath.section == 1){
cell.backgroundColor = [UIColor clearColor];

}
}

2014/02/09 10:38:22

その他の回答0件)

id:bg5551 No.1

回答回数1184ベストアンサー獲得回数80ここでベストアンサー

ポイント100pt

こちらの方法ではいかがでしょうか。

http://umeji.blogspot.jp/2010_09_01_archive.html

id:ankomoti3260

コメントありがとうございます。
ご紹介いただきました方法ですと、全ての境界線が削除されてしまいます。
質問内容がわかりにくくすみませんでした。
やりたかったのは、2セクションのうち2つ目のセクションのセルの境界線を削除したく、fiwaさんご紹介のコードから下記のコードで出来るようになりました。


  • (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return 2;
}

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

if( section == 0){
return 2;
}else{
return 1;
}
return 1;
}

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (!cell) { // yes
// セルを作成
cell = UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}

//cell.separatorInset = UIEdgeInsetsMake(0,10000,0,0);

if(indexPath.section == 0){
if (indexPath.row == 0) {
UIView *line = UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1)];
line.backgroundColor = [UIColor colorWithRed:0.82f green:0.82f blue:0.82f alpha:1.0f];
[cell addSubview:line];
//[cell bringSubviewToFront:line];
} else if (indexPath.row == 2 - 1) {
UIView *line = UIView alloc] initWithFrame:CGRectMake(21, 0, 320, 1)];
line.backgroundColor = [UIColor colorWithRed:0.82f green:0.82f blue:0.82f alpha:1.0f];
[cell addSubview:line];
//[cell bringSubviewToFront:line];

UIView *lineBottom = UIView alloc] initWithFrame:CGRectMake(0, 43, 320, 1)];
lineBottom.backgroundColor = [UIColor colorWithRed:0.82f green:0.82f blue:0.82f alpha:1.0f];
[cell addSubview:lineBottom];
//[cell bringSubviewToFront:lineBottom];
}
}

cell.textLabel.text = @"test";
return cell;
}


  • (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{
if(indexPath.section == 1){
cell.backgroundColor = [UIColor clearColor];

}
}

2014/02/09 10:38:22
id:ankomoti3260

最終的には下記のコードで特定のセクション内のセルの境界線を削除できるようになりました。

  • (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return 2;

}

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

if( section == 0){

return 2;

}else{

return 1;

}

return 1;

}

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

NSString *cellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (!cell) { // yes

// セルを作成

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

}

//cell.separatorInset = UIEdgeInsetsMake(0,10000,0,0);

if(indexPath.section == 0){

if (indexPath.row == 0) {

UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1)];

line.backgroundColor = [UIColor colorWithRed:0.82f green:0.82f blue:0.82f alpha:1.0f];

[cell addSubview:line];

//[cell bringSubviewToFront:line];

} else if (indexPath.row == 2 - 1) {

UIView *line = [[UIView alloc] initWithFrame:CGRectMake(21, 0, 320, 1)];

line.backgroundColor = [UIColor colorWithRed:0.82f green:0.82f blue:0.82f alpha:1.0f];

[cell addSubview:line];

//[cell bringSubviewToFront:line];

UIView *lineBottom = [[UIView alloc] initWithFrame:CGRectMake(0, 43, 320, 1)];

lineBottom.backgroundColor = [UIColor colorWithRed:0.82f green:0.82f blue:0.82f alpha:1.0f];

[cell addSubview:lineBottom];

//[cell bringSubviewToFront:lineBottom];

}

}

cell.textLabel.text = @"test";

return cell;

}



  • (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{

if(indexPath.section == 1){

cell.backgroundColor = [UIColor clearColor];

}

}

  • id:fiwa
    このあたりとか、紹介されてるStackOverflowの質問が参考になるかも?
    http://wata.blog.jp/archives/1827423.html
  • id:ankomoti3260
    コメントありがとうございます。
    また返答が遅くなりすみません
    紹介されているスタックオーバーフローのコードで
    特定のセクションのセルの境界線を削除できるようになりました。

    Viewを作成して、CellにaddSubViewするんですね
    勉強になりました。

この質問への反応(ブックマークコメント)

「あの人に答えてほしい」「この質問はあの人が答えられそう」というときに、回答リクエストを送ってみてましょう。

これ以上回答リクエストを送信することはできません。制限について

回答リクエストを送信したユーザーはいません