building conway's game of life for iPad [closed] - ipad

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I'm trying to build conway's game of life in Xcode (for the iPad), but I can't seem to figure out how to make it reasonably sized. As it is now its 10*10 and took quite a while to put together. I don't even think this works right.
- (void) drawRect: (CGRect) rect {
if (first == false) {
first = true;
for (int x = 0; x < 10; ++x) {
for (int y = 0; y < 10; ++y) {
ask[x][y] = false;
}
}
// ask[7][7] = true;
ask[3][9] = true;
ask[3][8] = true;
ask[4][9] = true;
ask[3][7] = true;
ask[5][2] = true;
ask[1][4] = true;
ask[2][3] = true;
ask[1][3] = true;
ask[4][2] = true;
}
for (int x = 0; x < 10; ++x) {
for (int y = 0; y < 10; ++y) {
counter1 = 0;
if((ask[x][y]) == true){
if(ask[x][y+1] == true){
counter1+= 1;
}
if(ask[x][y-1] == true){
counter1+= 1;
}
if(ask[x-1][y-1] == true){
counter1+= 1;
}
if(ask[x-1][y+1] == true){
counter1+= 1;
}
if ((ask[x-1][y] = true)){
counter1+= 1;
}
if(ask[x+1][y+1] == true){
counter1+= 1;
}
if(ask[x+1][y] == true){
counter1+= 1;
}
if(ask[x+1][y-1] == true) {
counter1+= 1;
}
if (counter1 < 2){
ask[x][y] = false;
}else if (counter1 > 3){
ask[x][y] = false;
}
}else if (ask[x][y] == false){
if(ask[x][y-1] == true){
counter1+= 1;
}
if(ask[x][y+1] == true){
counter1+= 1;
}
if ((ask[x-1][y] = true)){
counter1+= 1;
}
if(ask[x-1][y-1] == true){
counter1+= 1;
}
if(ask[x-1][y+1] == true){
counter1+= 1;
}
if(ask[x+1][y+1] == true){
counter1+= 1;
}
if(ask[x+1][y] == true){
counter1+= 1;
}
if(ask[x+1][y-1] == true) {
counter1+= 1;
}
if (counter1 == 3) {
ask[x][y] = true;
}
}
}
}
if (ask[0][0] == false) {
one = [UIImage imageNamed:#"w.png"];
}else{
one = [UIImage imageNamed:#"b.png"];
}
if (ask[0][1] == false) {
two = [UIImage imageNamed:#"w.png"];
}else{
two = [UIImage imageNamed:#"b.png"];
}
if (ask[0][2] == false) {
three = [UIImage imageNamed:#"w.png"];
}else{
three = [UIImage imageNamed:#"b.png"];
}
if (ask[0][3] == false) {
four = [UIImage imageNamed:#"w.png"];
}else{
four = [UIImage imageNamed:#"b.png"];
}
if (ask[0][4] == false) {
five = [UIImage imageNamed:#"w.png"];
}else{
five = [UIImage imageNamed:#"b.png"];
}
if (ask[0][5] == false) {
six = [UIImage imageNamed:#"w.png"];
}else{
six = [UIImage imageNamed:#"b.png"];
}
if (ask[0][6] == false) {
seven = [UIImage imageNamed:#"w.png"];
}else{
seven = [UIImage imageNamed:#"b.png"];
}
if (ask[0][7] == false) {
eight = [UIImage imageNamed:#"w.png"];
}else{
eight = [UIImage imageNamed:#"b.png"];
}
if (ask[0][8] == false) {
nine = [UIImage imageNamed:#"w.png"];
}else{
nine = [UIImage imageNamed:#"b.png"];
}
if (ask[0][9] == false) {
ten = [UIImage imageNamed:#"w.png"];
}else{
ten = [UIImage imageNamed:#"b.png"];
}
if (ask[1][0] == false) {
eleven = [UIImage imageNamed:#"w.png"];
}else{
eleven = [UIImage imageNamed:#"b.png"];
}
if (ask[1][1] == false) {
twelve = [UIImage imageNamed:#"w.png"];
}else{
twelve = [UIImage imageNamed:#"b.png"];
}
if (ask[1][2] == false) {
thirteen = [UIImage imageNamed:#"w.png"];
}else{
thirteen = [UIImage imageNamed:#"b.png"];
}
if (ask[1][3] == false) {
fourteen = [UIImage imageNamed:#"w.png"];
}else{
fourteen = [UIImage imageNamed:#"b.png"];
}
if (ask[1][4] == false) {
fifteen = [UIImage imageNamed:#"w.png"];
}else{
fifteen = [UIImage imageNamed:#"b.png"];
}
if (ask[1][5] == false) {
sixteen = [UIImage imageNamed:#"w.png"];
}else{
sixteen = [UIImage imageNamed:#"b.png"];
}
if (ask[1][6] == false) {
seventeen = [UIImage imageNamed:#"w.png"];
}else{
seventeen = [UIImage imageNamed:#"b.png"];
}
if (ask[1][7] == false) {
eighteen = [UIImage imageNamed:#"w.png"];
}else{
eighteen = [UIImage imageNamed:#"b.png"];
}
if (ask[1][8] == false) {
nineteen = [UIImage imageNamed:#"w.png"];
}else{
nineteen = [UIImage imageNamed:#"b.png"];
}
if (ask[1][9] == false) {
twenty = [UIImage imageNamed:#"w.png"];
}else{
twenty = [UIImage imageNamed:#"b.png"];
}
if (ask[2][0] == false) {
twentyone = [UIImage imageNamed:#"w.png"];
}else{
twentyone = [UIImage imageNamed:#"b.png"];
}
if (ask[2][1] == false) {
twentytwo = [UIImage imageNamed:#"w.png"];
}else{
twentytwo = [UIImage imageNamed:#"b.png"];
}
if (ask[2][2] == false) {
twentythree = [UIImage imageNamed:#"w.png"];
}else{
twentythree = [UIImage imageNamed:#"b.png"];
}
if (ask[2][3] == false) {
twentyfour = [UIImage imageNamed:#"w.png"];
}else{
twentyfour = [UIImage imageNamed:#"b.png"];
}
if (ask[2][4] == false) {
twentyfive = [UIImage imageNamed:#"w.png"];
}else{
twentyfive = [UIImage imageNamed:#"b.png"];
}
if (ask[2][5] == false) {
twentysix = [UIImage imageNamed:#"w.png"];
}else{
twentysix = [UIImage imageNamed:#"b.png"];
}
if (ask[2][6] == false) {
twentyseven = [UIImage imageNamed:#"w.png"];
}else{
twentyseven = [UIImage imageNamed:#"b.png"];
}
if (ask[2][7] == false) {
twentyeight = [UIImage imageNamed:#"w.png"];
}else{
twentyeight = [UIImage imageNamed:#"b.png"];
}
if (ask[2][8] == false) {
twentynine = [UIImage imageNamed:#"w.png"];
}else{
twentynine = [UIImage imageNamed:#"b.png"];
}
if (ask[2][9] == false) {
thirty = [UIImage imageNamed:#"w.png"];
}else{
thirty = [UIImage imageNamed:#"b.png"];
}
if (ask[3][0] == false) {
thirtyone = [UIImage imageNamed:#"w.png"];
}else{
thirtyone = [UIImage imageNamed:#"b.png"];
}
if (ask[3][1] == false) {
thirtytwo = [UIImage imageNamed:#"w.png"];
}else{
thirtytwo = [UIImage imageNamed:#"b.png"];
}
if (ask[3][2] == false) {
thirtythree = [UIImage imageNamed:#"w.png"];
}else{
thirtythree = [UIImage imageNamed:#"b.png"];
}
if (ask[3][3] == false) {
thirtyfour = [UIImage imageNamed:#"w.png"];
}else{
thirtyfour = [UIImage imageNamed:#"b.png"];
}
if (ask[3][4] == false) {
thirtyfive = [UIImage imageNamed:#"w.png"];
}else{
thirtyfive = [UIImage imageNamed:#"b.png"];
}
if (ask[3][5] == false) {
thirtysix = [UIImage imageNamed:#"w.png"];
}else{
thirtysix = [UIImage imageNamed:#"b.png"];
}
if (ask[3][6] == false) {
thirtyseven = [UIImage imageNamed:#"w.png"];
}else{
thirtyseven = [UIImage imageNamed:#"b.png"];
}
if (ask[3][7] == false) {
thirtyeight = [UIImage imageNamed:#"w.png"];
}else{
thirtyeight = [UIImage imageNamed:#"b.png"];
}
if (ask[3][8] == false) {
thirtynine = [UIImage imageNamed:#"w.png"];
}else{
thirtynine = [UIImage imageNamed:#"b.png"];
}
if (ask[3][9] == false) {
forty = [UIImage imageNamed:#"w.png"];
}else{
forty = [UIImage imageNamed:#"b.png"];
}
if (ask[4][0] == false) {
forty1 = [UIImage imageNamed:#"w.png"];
}else{
forty1 = [UIImage imageNamed:#"b.png"];
}
if (ask[4][1] == false) {
forty2 = [UIImage imageNamed:#"w.png"];
}else{
forty2 = [UIImage imageNamed:#"b.png"];
}
if (ask[4][2] == false) {
forty3 = [UIImage imageNamed:#"w.png"];
}else{
forty3 = [UIImage imageNamed:#"b.png"];
}
if (ask[4][3] == false) {
forty4 = [UIImage imageNamed:#"w.png"];
}else{
forty4 = [UIImage imageNamed:#"b.png"];
}
if (ask[4][4] == false) {
forty5 = [UIImage imageNamed:#"w.png"];
}else{
forty5 = [UIImage imageNamed:#"b.png"];
}
if (ask[4][5] == false) {
forty6 = [UIImage imageNamed:#"w.png"];
}else{
forty6 = [UIImage imageNamed:#"b.png"];
}
if (ask[4][6] == false) {
forty7 = [UIImage imageNamed:#"w.png"];
}else{
forty7 = [UIImage imageNamed:#"b.png"];
}
if (ask[4][7] == false) {
forty8 = [UIImage imageNamed:#"w.png"];
}else{
forty8 = [UIImage imageNamed:#"b.png"];
}
if (ask[4][8] == false) {
forty9 = [UIImage imageNamed:#"w.png"];
}else{
forty9 = [UIImage imageNamed:#"b.png"];
}
if (ask[4][9] == false) {
fifty = [UIImage imageNamed:#"w.png"];
}else{
fifty = [UIImage imageNamed:#"b.png"];
}
if (ask[5][0] == false) {
fifty1 = [UIImage imageNamed:#"w.png"];
}else{
fifty1 = [UIImage imageNamed:#"b.png"];
}
if (ask[5][1] == false) {
fifty2 = [UIImage imageNamed:#"w.png"];
}else{
fifty2 = [UIImage imageNamed:#"b.png"];
}
if (ask[5][2] == false) {
fifty3 = [UIImage imageNamed:#"w.png"];
}else{
fifty3 = [UIImage imageNamed:#"b.png"];
}
if (ask[5][3] == false) {
fifty4 = [UIImage imageNamed:#"w.png"];
}else{
fifty4 = [UIImage imageNamed:#"b.png"];
}
if (ask[5][4] == false) {
fifty5 = [UIImage imageNamed:#"w.png"];
}else{
fifty5 = [UIImage imageNamed:#"b.png"];
}
if (ask[5][5] == false) {
fifty6 = [UIImage imageNamed:#"w.png"];
}else{
fifty6 = [UIImage imageNamed:#"b.png"];
}
if (ask[5][6] == false) {
fifty7 = [UIImage imageNamed:#"w.png"];
}else{
fifty7 = [UIImage imageNamed:#"b.png"];
}
if (ask[5][7] == false) {
fifty8 = [UIImage imageNamed:#"w.png"];
}else{
fifty8 = [UIImage imageNamed:#"b.png"];
}
if (ask[5][8] == false) {
fifty9 = [UIImage imageNamed:#"w.png"];
}else{
fifty9 = [UIImage imageNamed:#"b.png"];
}
if (ask[5][9] == false) {
sixty = [UIImage imageNamed:#"w.png"];
}else{
sixty = [UIImage imageNamed:#"b.png"];
}
if (ask[6][0] == false) {
sixty1 = [UIImage imageNamed:#"w.png"];
}else{
sixty1 = [UIImage imageNamed:#"b.png"];
}
if (ask[6][1] == false) {
sixty2 = [UIImage imageNamed:#"w.png"];
}else{
sixty2 = [UIImage imageNamed:#"b.png"];
}
if (ask[6][2] == false) {
sixty3 = [UIImage imageNamed:#"w.png"];
}else{
sixty3 = [UIImage imageNamed:#"b.png"];
}
if (ask[6][3] == false) {
sixty4 = [UIImage imageNamed:#"w.png"];
}else{
sixty4 = [UIImage imageNamed:#"b.png"];
}
if (ask[6][4] == false) {
sixty5 = [UIImage imageNamed:#"w.png"];
}else{
sixty5 = [UIImage imageNamed:#"b.png"];
}
if (ask[6][5] == false) {
sixty6 = [UIImage imageNamed:#"w.png"];
}else{
sixty6 = [UIImage imageNamed:#"b.png"];
}
if (ask[6][6] == false) {
sixty7 = [UIImage imageNamed:#"w.png"];
}else{
sixty7 = [UIImage imageNamed:#"b.png"];
}
if (ask[6][7] == false) {
sixty8 = [UIImage imageNamed:#"w.png"];
}else{
sixty8 = [UIImage imageNamed:#"b.png"];
}
if (ask[6][8] == false) {
sixty9 = [UIImage imageNamed:#"w.png"];
}else{
sixty9 = [UIImage imageNamed:#"b.png"];
}
if (ask[6][9] == false) {
seventy = [UIImage imageNamed:#"w.png"];
}else{
seventy = [UIImage imageNamed:#"b.png"];
}
if (ask[7][0] == false) {
seventy1 = [UIImage imageNamed:#"w.png"];
}else{
seventy1 = [UIImage imageNamed:#"b.png"];
}
if (ask[7][1] == false) {
seventy2 = [UIImage imageNamed:#"w.png"];
}else{
seventy2 = [UIImage imageNamed:#"b.png"];
}
if (ask[7][2] == false) {
seventy3 = [UIImage imageNamed:#"w.png"];
}else{
seventy3 = [UIImage imageNamed:#"b.png"];
}
if (ask[7][3] == false) {
seventy4 = [UIImage imageNamed:#"w.png"];
}else{
seventy4 = [UIImage imageNamed:#"b.png"];
}
if (ask[7][4] == false) {
seventy5 = [UIImage imageNamed:#"w.png"];
}else{
seventy5 = [UIImage imageNamed:#"b.png"];
}
if (ask[7][5] == false) {
seventy6 = [UIImage imageNamed:#"w.png"];
}else{
seventy6 = [UIImage imageNamed:#"b.png"];
}
if (ask[7][6] == false) {
seventy7 = [UIImage imageNamed:#"w.png"];
}else{
seventy7 = [UIImage imageNamed:#"b.png"];
}
if (ask[7][7] == false) {
seventy8 = [UIImage imageNamed:#"w.png"];
}else{
seventy8 = [UIImage imageNamed:#"b.png"];
}
if (ask[7][8] == false) {
seventy9 = [UIImage imageNamed:#"w.png"];
}else{
seventy9 = [UIImage imageNamed:#"b.png"];
}
if (ask[7][9] == false) {
eighty = [UIImage imageNamed:#"w.png"];
}else{
eighty = [UIImage imageNamed:#"b.png"];
}
if (ask[8][0] == false) {
eighty1 = [UIImage imageNamed:#"w.png"];
}else{
eighty1 = [UIImage imageNamed:#"b.png"];
}
if (ask[8][1] == false) {
eighty2 = [UIImage imageNamed:#"w.png"];
}else{
eighty2 = [UIImage imageNamed:#"b.png"];
}
if (ask[8][2] == false) {
eighty3 = [UIImage imageNamed:#"w.png"];
}else{
eighty3 = [UIImage imageNamed:#"b.png"];
}
if (ask[8][3] == false) {
eighty4 = [UIImage imageNamed:#"w.png"];
}else{
eighty4 = [UIImage imageNamed:#"b.png"];
}
if (ask[8][4] == false) {
eighty5 = [UIImage imageNamed:#"w.png"];
}else{
eighty5 = [UIImage imageNamed:#"b.png"];
}
if (ask[8][5] == false) {
eighty6 = [UIImage imageNamed:#"w.png"];
}else{
eighty6 = [UIImage imageNamed:#"b.png"];
}
if (ask[8][6] == false) {
eighty7 = [UIImage imageNamed:#"w.png"];
}else{
eighty7 = [UIImage imageNamed:#"b.png"];
}
if (ask[8][7] == false) {
eighty8 = [UIImage imageNamed:#"w.png"];
}else{
eighty8 = [UIImage imageNamed:#"b.png"];
}
if (ask[8][8] == false) {
eighty9 = [UIImage imageNamed:#"w.png"];
}else{
eighty9 = [UIImage imageNamed:#"b.png"];
}
if (ask[8][9] == false) {
ninety = [UIImage imageNamed:#"w.png"];
}else{
ninety = [UIImage imageNamed:#"b.png"];
}
if (ask[9][0] == false) {
ninety1 = [UIImage imageNamed:#"w.png"];
}else{
ninety1 = [UIImage imageNamed:#"b.png"];
}
if (ask[9][1] == false) {
ninety2 = [UIImage imageNamed:#"w.png"];
}else{
ninety2 = [UIImage imageNamed:#"b.png"];
}
if (ask[9][2] == false) {
ninety3 = [UIImage imageNamed:#"w.png"];
}else{
ninety3 = [UIImage imageNamed:#"b.png"];
}
if (ask[9][3] == false) {
ninety4 = [UIImage imageNamed:#"w.png"];
}else{
ninety4 = [UIImage imageNamed:#"b.png"];
}
if (ask[9][4] == false) {
ninety5 = [UIImage imageNamed:#"w.png"];
}else{
ninety5 = [UIImage imageNamed:#"b.png"];
}
if (ask[9][5] == false) {
ninety6 = [UIImage imageNamed:#"w.png"];
}else{
ninety6 = [UIImage imageNamed:#"b.png"];
}
if (ask[9][6] == false) {
ninety7 = [UIImage imageNamed:#"w.png"];
}else{
ninety7 = [UIImage imageNamed:#"b.png"];
}
if (ask[9][7] == false) {
ninety8 = [UIImage imageNamed:#"w.png"];
}else{
ninety8 = [UIImage imageNamed:#"b.png"];
}
if (ask[9][8] == false) {
ninety9 = [UIImage imageNamed:#"w.png"];
}else{
ninety9 = [UIImage imageNamed:#"b.png"];
}
if (ask[9][9] == false) {
hundred = [UIImage imageNamed:#"w.png"];
}else{
hundred = [UIImage imageNamed:#"b.png"];
}
[one drawAtPoint:CGPointMake(100, 0)];
[two drawAtPoint:CGPointMake(100, 25)];
[three drawAtPoint:CGPointMake(100, 50)];
[four drawAtPoint:CGPointMake(100, 75)];
[five drawAtPoint:CGPointMake(100, 100)];
[six drawAtPoint:CGPointMake(100, 125)];
[seven drawAtPoint:CGPointMake(100, 150)];
[eight drawAtPoint:CGPointMake(100, 175)];
[nine drawAtPoint:CGPointMake(100, 200)];
[ten drawAtPoint:CGPointMake(100, 225)];
[eleven drawAtPoint:CGPointMake(125, 0)];
[twelve drawAtPoint:CGPointMake(125, 25)];
[thirteen drawAtPoint:CGPointMake(125, 50)];
[fourteen drawAtPoint:CGPointMake(125, 75)];
[fifteen drawAtPoint:CGPointMake(125, 100)];
[sixteen drawAtPoint:CGPointMake(125, 125)];
[seventeen drawAtPoint:CGPointMake(125, 150)];
[eighteen drawAtPoint:CGPointMake(125, 175)];
[nineteen drawAtPoint:CGPointMake(125, 200)];
[twenty drawAtPoint:CGPointMake(125, 225)];
[twentyone drawAtPoint:CGPointMake(150, 0)];
[twentytwo drawAtPoint:CGPointMake(150, 25)];
[twentythree drawAtPoint:CGPointMake(150, 50)];
[twentyfour drawAtPoint:CGPointMake(150, 75)];
[twentyfive drawAtPoint:CGPointMake(150, 100)];
[twentysix drawAtPoint:CGPointMake(150, 125)];
[twentyseven drawAtPoint:CGPointMake(150, 150)];
[twentyeight drawAtPoint:CGPointMake(150, 175)];
[twentynine drawAtPoint:CGPointMake(150, 200)];
[thirty drawAtPoint:CGPointMake(150, 225)];
[thirtyone drawAtPoint:CGPointMake(175, 0)];
[thirtytwo drawAtPoint:CGPointMake(175, 25)];
[thirtythree drawAtPoint:CGPointMake(175, 50)];
[thirtyfour drawAtPoint:CGPointMake(175, 75)];
[thirtyfive drawAtPoint:CGPointMake(175, 100)];
[thirtysix drawAtPoint:CGPointMake(175, 125)];
[thirtyseven drawAtPoint:CGPointMake(175, 150)];
[thirtyeight drawAtPoint:CGPointMake(175, 175)];
[thirtynine drawAtPoint:CGPointMake(175, 200)];
[forty drawAtPoint:CGPointMake(175, 225)];
[forty1 drawAtPoint:CGPointMake(200, 0)];
[forty2 drawAtPoint:CGPointMake(200, 25)];
[forty3 drawAtPoint:CGPointMake(200, 50)];
[forty4 drawAtPoint:CGPointMake(200, 75)];
[forty5 drawAtPoint:CGPointMake(200, 100)];
[forty6 drawAtPoint:CGPointMake(200, 125)];
[forty7 drawAtPoint:CGPointMake(200, 150)];
[forty8 drawAtPoint:CGPointMake(200, 175)];
[forty9 drawAtPoint:CGPointMake(200, 200)];
[fifty drawAtPoint:CGPointMake(200, 225)];
[fifty1 drawAtPoint:CGPointMake(225, 0)];
[fifty2 drawAtPoint:CGPointMake(225, 25)];
[fifty3 drawAtPoint:CGPointMake(225, 50)];
[fifty4 drawAtPoint:CGPointMake(225, 75)];
[fifty5 drawAtPoint:CGPointMake(225, 100)];
[fifty6 drawAtPoint:CGPointMake(225, 125)];
[fifty7 drawAtPoint:CGPointMake(225, 150)];
[fifty8 drawAtPoint:CGPointMake(225, 175)];
[fifty9 drawAtPoint:CGPointMake(225, 200)];
[sixty drawAtPoint:CGPointMake(225, 225)];
[sixty1 drawAtPoint:CGPointMake(250, 0)];
[sixty2 drawAtPoint:CGPointMake(250, 25)];
[sixty3 drawAtPoint:CGPointMake(250, 50)];
[sixty4 drawAtPoint:CGPointMake(250, 75)];
[sixty5 drawAtPoint:CGPointMake(250, 100)];
[sixty6 drawAtPoint:CGPointMake(250, 125)];
[sixty7 drawAtPoint:CGPointMake(250, 150)];
[sixty8 drawAtPoint:CGPointMake(250, 175)];
[sixty9 drawAtPoint:CGPointMake(250, 200)];
[seventy drawAtPoint:CGPointMake(250, 225)];
[seventy1 drawAtPoint:CGPointMake(275, 0)];
[seventy2 drawAtPoint:CGPointMake(275, 25)];
[seventy3 drawAtPoint:CGPointMake(275, 50)];
[seventy4 drawAtPoint:CGPointMake(275, 75)];
[seventy5 drawAtPoint:CGPointMake(275, 100)];
[seventy6 drawAtPoint:CGPointMake(275, 125)];
[seventy7 drawAtPoint:CGPointMake(275, 150)];
[seventy8 drawAtPoint:CGPointMake(275, 175)];
[seventy9 drawAtPoint:CGPointMake(275, 200)];
[eighty drawAtPoint:CGPointMake(275, 225)];
[eighty1 drawAtPoint:CGPointMake(300, 0)];
[eighty2 drawAtPoint:CGPointMake(300, 25)];
[eighty3 drawAtPoint:CGPointMake(300, 50)];
[eighty4 drawAtPoint:CGPointMake(300, 75)];
[eighty5 drawAtPoint:CGPointMake(300, 100)];
[eighty6 drawAtPoint:CGPointMake(300, 125)];
[eighty7 drawAtPoint:CGPointMake(300, 150)];
[eighty8 drawAtPoint:CGPointMake(300, 175)];
[eighty9 drawAtPoint:CGPointMake(300, 200)];
[ninety drawAtPoint:CGPointMake(300, 225)];
[ninety1 drawAtPoint:CGPointMake(325, 0)];
[ninety2 drawAtPoint:CGPointMake(325, 25)];
[ninety3 drawAtPoint:CGPointMake(325, 50)];
[ninety4 drawAtPoint:CGPointMake(325, 75)];
[ninety5 drawAtPoint:CGPointMake(325, 100)];
[ninety6 drawAtPoint:CGPointMake(325, 125)];
[ninety7 drawAtPoint:CGPointMake(325, 150)];
[ninety8 drawAtPoint:CGPointMake(325, 175)];
[ninety9 drawAtPoint:CGPointMake(325, 200)];
[hundred drawAtPoint:CGPointMake(325, 225)];}
-(void) dealloc {
}
#end

Yes, several ideas. :D
http://www.oliverfoggin.com/game-of-life/
The way I would approach building it (if I was to start again), would be to work exclusively on a class (or collection of classes) for the model of the game. i.e. the engine. (I may even put a cellular automaton engine on github).
Anyway, I presume you have looked into how the game of life works?
Size of "world"
The first thing you'll have to decide is how big you want to make the "world" in your app. I would suggest going for a finite size over an infinite size as it makes it orders of magnitude less complex.
Model
You'll need to store your cells in some sort of data structure. This could be a 2D array or something else. My initial version was a 2D array of bool values (true = alive, false = dead).
Counting Neighbours
You'll need a way of counting the number of alive neighbours for each cell.
Iterating a generation
Once you have all the above you should then be able to use these to advance your map by one generation.
Like I said, start with the data model and basic functions before you do anything visual for the app.
Once you've done that you can start working out how to display your model.
It's not a simple task and you may well have around "one thousand" lines of code at the end for you whole project.
The best way though is to dive in and just try and write it.

Related

Adding NSTimer in image slide and page contol to move images automatically

Here is the image slider works fine when dragging manually the image changes with page control indicator change but I want to add the timer to the following code and move the images and indicator automatically. Please help me to apply NS timer in this code and also want to move from the last image to 1st image and so on.
#implementation DashboardViewController {
NSArray * animationArray;
}
#synthesize scroller = scroller;
#synthesize pageControl = pageControl;
-(void) viewDidLoad {
[super viewDidLoad];
scroller.pagingEnabled = YES;
scroller.showsHorizontalScrollIndicator = NO;
scroller.delegate = self;
animationArray = [NSArray arrayWithObjects: [UIImage imageNamed: # "image1.jpg"],
[UIImage imageNamed: # "image2.jpg"],
[UIImage imageNamed: # "image3.jpg"],
[UIImage imageNamed: # "image4.png"],
[UIImage imageNamed: # "image5.jpg"],
nil
];
CGRect scrollFrame = CGRectMake(0, 0, self.view.frame.size.width, self.scroller.frame.size.height);
scroller.frame = scrollFrame;
self.scroller.contentSize = CGSizeMake(self.scroller.frame.size.width * animationArray.count, self.scroller.frame.size.height);
for (int i = 0; i < animationArray.count; i++) {
CGRect frame;
frame.origin.x = self.scroller.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scroller.frame.size;
UIImageView * imgView = [
[UIImageView alloc] initWithFrame: CGRectMake(self.scroller.frame.size.width * i, 0, self.scroller.frame.size.width, self.scroller.frame.size.height)
];
imgView.image = [animationArray objectAtIndex: i];
imgView.frame = frame;
[self.scroller addSubview: imgView];
}
self.pageControl.currentPage = 0;
}
-(void) scrollViewDidScroll: (UIScrollView * ) sender {
if (!pageControlBeingUsed) {
// Switch the indicator when more than 50% of the previous/next page is visible
CGFloat pageWidth = self.scroller.frame.size.width;
int page = floor((self.scroller.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = page;
}
}
-(void) scrollViewWillBeginDragging: (UIScrollView * ) scrollView {
pageControlBeingUsed = NO;
}
-(void) scrollViewDidEndDecelerating: (UIScrollView * ) scrollView {
[self setIndiactorForCurrentPage];
}
-(void) setIndiactorForCurrentPage {
uint page = scroller.contentOffset.x / scroller.frame.size.width;
[pageControl setCurrentPage: page];
}
- (IBAction) changePage {
// Update the scroll view to the appropriate page
CGRect frame;
pageControl.currentPage = animationArray.count;
frame.origin.x = self.scroller.frame.size.width * self.pageControl.currentPage;
frame.origin.y = 0;
frame.size = self.scroller.frame.size;
[self.scroller scrollRectToVisible: frame animated: YES];
pageControlBeingUsed = YES;
}
- (void) didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
self.scroller = nil;
self.pageControl = nil;
}
Please try this code for NSTimer (i use collectionView for easy use)
var timer : Timer?
var currentFeaturedCouponIndex = 0
//MARK: - NSTimer -
func setupTimer(){
timer = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(self.scrollFeatured), userInfo: nil, repeats: true);
}
func scrollFeatured(){
if self.arrFeaturedCoupon.count > 0 {
//Becauese we scroll coupon if total is > 1
if self.arrFeaturedCoupon.count > 1 {
if self.currentFeaturedCouponIndex < self.arrFeaturedCoupon.count - 1 {
self.currentFeaturedCouponIndex = self.currentFeaturedCouponIndex + 1
}
else {
self.currentFeaturedCouponIndex = 0
}
self.pageControl.currentPage = currentFeaturedCouponIndex
//When currentFeaturedCouponIndex == 0 we make animated: false
if self.currentFeaturedCouponIndex == 0 {
self.featuredCouponColView.scrollToItem(at: self.featuredCouponCurrentIndexPath, at: UICollectionViewScrollPosition.left, animated: false)
}
else {
self.featuredCouponColView.scrollToItem(at: self.featuredCouponCurrentIndexPath, at: UICollectionViewScrollPosition.left, animated: true)
}
}
}
}
To invalidate timer when and where you want
func invalidateTimer(){
if timer != nil {
self.timer!.invalidate()
self.timer = nil
}
}

How to change color and size of title for cluster marker in GoogleMaps iOS?

In my case cluster custome image is white background and I need to change color of text and make it more smaller
- (void)configureMap {
// Set up the cluster manager with a supplied icon generator and renderer.
id<GMUClusterAlgorithm> algorithm = [[GMUNonHierarchicalDistanceBasedAlgorithm alloc] init];
id<GMUClusterIconGenerator> iconGenerator = [self iconGeneratorWithImages];
id<GMUClusterRenderer> renderer = [[GMUDefaultClusterRenderer alloc] initWithMapView:_mapView clusterIconGenerator:iconGenerator];
_clusterManager = [[GMUClusterManager alloc] initWithMap:_mapView
algorithm:algorithm
renderer:renderer];
}
- (id<GMUClusterIconGenerator>)iconGeneratorWithImages {
UIImage *clusterImage = [UIImage imageNamed:#"customClusterImage"];
return [[GMUDefaultClusterIconGenerator alloc] initWithBuckets:#[#10, #50, #100, #200, #1000]
backgroundImages:#[clusterImage, clusterImage, clusterImage, clusterImage, clusterImage]
];
}
UPD: no answers here: https://github.com/googlemaps/google-maps-ios-utils/issues/127
- (void)configureMapView {
// ....
id<GMUClusterAlgorithm> algorithm = [[GMUNonHierarchicalDistanceBasedAlgorithm alloc] init];
id<GMUClusterIconGenerator> iconGenerator = [[GMUDefaultClusterIconGenerator alloc] init];
GMUDefaultClusterRenderer *renderer = [[GMUDefaultClusterRenderer alloc] initWithMapView:_mapView clusterIconGenerator:iconGenerator];
renderer.delegate = self; // <---- 1. set delegate
_clusterManager = [[GMUClusterManager alloc] initWithMap:_mapView
algorithm:algorithm
renderer:renderer];
}
#pragma mark - <GMUClusterRendererDelegate>
- (void)renderer:(id<GMUClusterRenderer>)renderer willRenderMarker:(GMSMarker *)marker {
if ([marker.userData isKindOfClass:[MyMarker class]]) {
marker.icon = [UIImage imageNamed:#"markerNormal"] // <----- 2.1 icon for ordinary marker
} else if ([marker.userData conformsToProtocol:#protocol(GMUCluster)]) {
// <------ 2.2 icon for cluster marker (draw title on image)
id<GMUCluster> userData = marker.userData;
marker.icon = [self drawFront:[UIImage imageNamed:#"markerGroup"] text:[NSString stringWithFormat:#"%#", #(userData.count)]];
}
}
#pragma mark -
- (UIImage *)drawFront:(UIImage *)image text:(NSString *)text {
// draw image first
UIGraphicsBeginImageContextWithOptions(image.size, false, [UIScreen mainScreen].scale);
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
// text attributes
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
[style setAlignment:NSTextAlignmentCenter];
NSDictionary *attr = #{NSParagraphStyleAttributeName : style,
NSFontAttributeName : [UIFont systemFontOfSize:17. weight:UIFontWeightRegular], // set text font
NSForegroundColorAttributeName : [UIColor redColor] // set text color
};
const CGFloat paddingTop = 8.;
const CGRect rect = CGRectMake(0, paddingTop, image.size.width., image.size.height);
[text drawInRect:rect withAttributes:attr];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
func renderer(_ renderer: GMUClusterRenderer, willRenderMarker marker: GMSMarker) {
if let m = (marker.userData as? POIItem) {
let vehicle = m.vehicle
if m.isPlate {
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 0, height: 2))
label.backgroundColor = .white
label.textColor = .black
label.text = " \(vehicle.plate) "
label.layer.borderColor = UIColor.gray.cgColor
label.layer.borderWidth = 1
label.layer.cornerRadius = 4
label.alpha = 0.9
label.textAlignment = .center
label.sizeToFit()
marker.groundAnchor = CGPoint(x: 0.5, y: 0.1)
marker.iconView = label
} else {
var icon = "\(vehicle.icon)"
if icon == "1" {
icon = "green/\(vehicle.angle)"
}
marker.icon = UIImage(named: icon)
}
}
else {
if let x = marker.userData as? GMUStaticCluster {
let lbl = UILabel(frame: CGRect(x: 0, y: 0, width: 48, height: 48))
lbl.text = "\(x.count)"
lbl.textColor = .white
lbl.textAlignment = .center
lbl.backgroundColor = .red
lbl.layer.cornerRadius = 24
lbl.layer.masksToBounds = true
marker.iconView = lbl
}
}
}

Error when try becomeFirstResponder call for UIMenuController

I have a messenger, so I want to show UIMenuController on cell long tap.
I need image view to catch long press, because I have text and media messages. But I get a crash becomeFirstResponder call after I received long press gesture on iOS 8(iOS 7 works well):
*** Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'child view controller:<UICompatibilityInputViewController: 0x13fd75b90> should have parent view controller:<FriendPlay.WLDialogViewController: 0x13fd54d10> but requested parent is:<UIInputWindowController: 0x140815000>'
This is the part of code:
override func handleLongPress(longPress: UILongPressGestureRecognizer!) {
let alpha: CGFloat = 0.6
let point = longPress.locationInView(tableView)
let indexPath = tableView.indexPathForRowAtPoint(point)
if indexPath != nil {
let cell = tableView.cellForRowAtIndexPath(indexPath!) as MessageCell
let convertedPoint = longPress.locationInView(cell.balloonImageView)
if CGRectContainsPoint(cell.balloonImageView.frame, convertedPoint) {
var myView = cell.balloonImageView
if cell.message.type == MessageType.Text {
cell.balloonImageView.alpha = alpha
}
else {
myView = cell.mediaImageView
cell.mediaImageView.alpha = alpha
}
if longPress.state == UIGestureRecognizerState.Began {
showMenuAtPoint(convertedPoint, myView: myView)
}
}
}
}
private func showMenuAtPoint(point: CGPoint, myView: MessageImageView) {
myView.becomeFirstResponder()// <--- that crashes
let rect = CGRectMake(point.x, point.y, 0, 0)
let mc = UIMenuController.sharedMenuController()
mc.setTargetRect(rect, inView: myView)
let sendItem = UIMenuItem(title: "Send", action: "sendAgain:")
mc.menuItems = [sendItem]
mc.setMenuVisible(true, animated: true)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "menuDidHide:", name: UIMenuControllerDidHideMenuNotification, object: nil)
}
func sendAgain(sender: AnyObject) {
println("Send!")
}
For class MessageImageView (inherited from UIImageView) I redefine becomeFirstResponder to return always true.
I initialize my cell in - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier messageMaxWidth:(CGFloat)messageMaxWidth
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier messageMaxWidth:(CGFloat)messageMaxWidth
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.messageMaxWidth = messageMaxWidth;
self.backgroundColor = [UIColor clearColor];
self.panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePan:)];
self.panGesture.delegate = self;
[self addGestureRecognizer:self.panGesture];
[self setInitialSizes];
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handleOrientationWillChandeNote:) name:UIApplicationWillChangeStatusBarFrameNotification object:nil];
}
return self;
}
- (void)setInitialSizes
{
if (self.containerView) {
[self.containerView removeFromSuperview];
}
if (self.timeLabel) {
[self.timeLabel removeFromSuperview];
}
self.userImageView = [[UIImageView alloc] init];
self.userImageView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
self.textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, self.messageMaxWidth, 0)];
self.timeLabel = [[UILabel alloc] init];
self.mediaImageView = [[SOmessageImageView alloc] init];
self.mediaOverlayView = [[UIView alloc] init];
self.balloonImageView = [[SOmessageImageView alloc] init];
self.balloonImageView.userInteractionEnabled = YES;
if (!CGSizeEqualToSize(self.userImageViewSize, CGSizeZero)) {
CGRect frame = self.userImageView.frame;
frame.size = self.userImageViewSize;
self.userImageView.frame = frame;
}
self.userImageView.contentMode = UIViewContentModeScaleAspectFill;
self.userImageView.clipsToBounds = YES;
self.userImageView.backgroundColor = [UIColor clearColor];
self.userImageView.layer.cornerRadius = 5;
if (!CGSizeEqualToSize(self.mediaImageViewSize, CGSizeZero)) {
CGRect frame = self.mediaImageView.frame;
frame.size = self.mediaImageViewSize;
self.mediaImageView.frame = frame;
}
self.mediaImageView.contentMode = UIViewContentModeScaleAspectFill;
self.mediaImageView.clipsToBounds = YES;
self.mediaImageView.backgroundColor = [UIColor clearColor];
self.mediaImageView.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleMediaTapped:)];
[self.mediaImageView addGestureRecognizer:tap];
self.mediaOverlayView.backgroundColor = [UIColor clearColor];
[self.mediaImageView addSubview:self.mediaOverlayView];
self.textView.textColor = [UIColor whiteColor];
self.textView.backgroundColor = [UIColor clearColor];
[self.textView setTextContainerInset:UIEdgeInsetsZero];
self.textView.textContainer.lineFragmentPadding = 0;
[self hideSubViews];
self.containerView = [[UIView alloc] initWithFrame:self.contentView.bounds];
self.containerView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
[self.contentView addSubview:self.containerView];
[self.containerView addSubview:self.balloonImageView];
[self.containerView addSubview:self.textView];
[self.containerView addSubview:self.mediaImageView];
[self.containerView addSubview:self.userImageView];
self.contentView.clipsToBounds = NO;
self.clipsToBounds = NO;
self.timeLabel.font = [UIFont fontWithName:#"AvenirNextCyr-Light" size:11];
self.timeLabel.textColor = [UIColor grayColor];
self.timeLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
UIImage *backgroundImage = [UIImage imageNamed:#"messagesDateBackground"];
if (backgroundImage) {
self.timeLabel.textColor = [UIColor whiteColor];
if (self.backgroundImageView) {
[self.backgroundImageView removeFromSuperview];
}
self.backgroundImageView = [[UIImageView alloc] initWithImage:[backgroundImage resizableImageWithCapInsets:UIEdgeInsetsMake(20, 20, 20, 20)]];
self.backgroundImageView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
[self.contentView addSubview:self.backgroundImageView];
}
[self.contentView addSubview:self.timeLabel];
}
Please give me some suggestions, how to fix this strange bug.
Your view controller probably has a property named inputView that is merely a subview, not an inputView as UIResponder interface expects it to be.
Starting with iOS 8 they check that UIResponder's inputView has no parent.

change color navigation controller in a popover

I'm having a problem with trying to change the colour of my navigation controller inside my popoverController.
I'm trying to do something like this:
if (self.popoverController == nil) {
ArtistsViewController *artistsViewController =
[[ArtistsViewController alloc]
initWithNibName:#"ArtistsViewController"
bundle:[NSBundle mainBundle]];
artistsViewController.navigationItem.title = #"Artists";
UINavigationController *navController =
[[UINavigationController alloc]
initWithRootViewController:artistsViewController];
UIPopoverController *popover =
[[UIPopoverController alloc]
initWithContentViewController:artistsViewController.navigationController];
artistsViewController.navigationController.navigationBar.tintColor=[UIColor greenColor];
popover.delegate = self;
[artistsViewController release];
[navController release];
self.popoverController = popover;
[popover release];
}
[self.popoverController
presentPopoverFromBarButtonItem:sender
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
but it doesn't work. any suggestions?
////////////////////////////////////////
////////////**SOLUTION**////////
////////////////////////////////////////
I'm gonna edit this post because i solved my problem and I think could be helpful share my solution here:
first of all, follow this sample:
http://mobiforge.com/designing/story/using-popoverview-ipad-app-development
When It is done go to step two.
The second step will be add a new popoverBackgroundViewClass:
Add new file in your project Objective class and call it 'CustomPopoverBackgroundView'
///////////////////////////////////////////////
////// CustomPopoverBackgroundView.h /////////
///////////////////////////////////////////////
#import < UIKit/UIKit.h >
#import < UIKit/UIPopoverBackgroundView.h >
#interface CustomPopoverBackgroundView : UIPopoverBackgroundView{
UIPopoverArrowDirection direction;
CGFloat offset;
}
#end
//////////////////////////////////////////////
////// CustomPopoverBackgroundView.m /////////
//////////////////////////////////////////////
#import "CustomPopoverBackgroundView.h"
#implementation CustomPopoverBackgroundView
- (void)layoutSubviews {
[super layoutSubviews];
CGFloat fullHeight = self.frame.size.height;
CGFloat fullWidth = self.frame.size.width;
CGFloat startingLeft = 0.0;
CGFloat startingTop = 0.0;
CGFloat arrowCoord = 0.0;
UIImage *arrow;
UIImageView *arrowView;
switch (self.arrowDirection) {
case UIPopoverArrowDirectionUp:
startingTop += 13.0;
fullHeight -= 13.0;
//the image line.png will be the corner
arrow = [UIImage imageNamed:#"line.png"];
arrowCoord = (self.frame.size.width / 2) - self.arrowOffset;
arrowView = [[[UIImageView alloc] initWithFrame:CGRectMake(arrowCoord, 0, 13.0, 13.0)] autorelease];
break;
case UIPopoverArrowDirectionDown:
fullHeight -= 13.0;
arrow = [UIImage imageNamed:#"line.png"];
arrowCoord = (self.frame.size.width / 2) - self.arrowOffset;
arrowView = [[[UIImageView alloc] initWithFrame:CGRectMake(arrowCoord, fullHeight, 13.0, 13.0)] autorelease];
break;
case UIPopoverArrowDirectionLeft:
startingLeft += 13.0;
fullWidth -= 13.0;
arrow = [UIImage imageNamed:#"line.png"];
arrowCoord = (self.frame.size.height / 2) - self.arrowOffset;
arrowView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, arrowCoord, 13.0, 13.0)] autorelease];
break;
case UIPopoverArrowDirectionRight:
fullWidth -= 13.0;
arrow = [UIImage imageNamed:#"line.png"];
arrowCoord = (self.frame.size.height / 2) - self.arrowOffset;
arrowView = [[[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width-13.0, arrowCoord, 13.0, 13.0)] autorelease];
break;
}
//this image will be your background
UIImage *bg = [[UIImage imageNamed:#"lineBack.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(8.0, 8.0, 8.0, 8.0)];
UIImageView *imageView = [[[UIImageView alloc] initWithFrame:CGRectMake(startingLeft, startingTop, fullWidth, fullHeight)] autorelease];
[imageView setImage:bg];
[arrowView setImage:arrow];
[self addSubview:imageView];
[self addSubview:arrowView];
}
- (CGFloat) arrowOffset {
return offset;
}
- (void) setArrowOffset:(CGFloat)arrowOffset {
offset = arrowOffset;
[self setNeedsLayout];
}
- (UIPopoverArrowDirection)arrowDirection {
return direction;
}
- (void)setArrowDirection:(UIPopoverArrowDirection)arrowDirection {
direction = arrowDirection;
[self setNeedsLayout];
}
+ (CGFloat)arrowHeight {
return 30.0;
}
+ (CGFloat)arrowBase {
return 30.0;
}
+ (UIEdgeInsets)contentViewInsets {
return UIEdgeInsetsMake(30.0, 30.0, 30.0, 30.0);
}
#end
Third step:
When It is done add this line in 'PopOverExample1ViewController.m':
import the new class:
#import " CustomPopoverBackgroundView.h "
-(IBAction) showMovies:(id) sender {
if (self.popoverController == nil) {
MoviesViewController *movies =
[[MoviesViewController alloc]
initWithNibName:#"MoviesViewController"
bundle:[NSBundle mainBundle]];
UIPopoverController *popover =
[[UIPopoverController alloc] initWithContentViewController:movies];
popover.delegate = self;
[movies release];
//THIS IS THE LINE THAT YOU HAVE TO ADD
popover.popoverBackgroundViewClass=[CustomPopoverBackgroundView class];
self.popoverController = popover;
[popover release];
}
[self.popoverController
presentPopoverFromBarButtonItem:sender
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
-(IBAction) btnShowMovies:(id) sender {
if (self.popoverController == nil) {
MoviesViewController *movies =
[[MoviesViewController alloc]
initWithNibName:#"MoviesViewController"
bundle:[NSBundle mainBundle]];
UIPopoverController *popover =
[[UIPopoverController alloc] initWithContentViewController:movies];
popover.delegate = self;
[movies release];
//THIS IS THE LINE THAT YOU HAVE TO ADD
popover.popoverBackgroundViewClass=[CustomPopoverBackgroundView class];
self.popoverController = popover;
[popover release];
}
CGRect popoverRect = [self.view convertRect:[btn frame]
fromView:[btn superview]];
popoverRect.size.width = MIN(popoverRect.size.width, 100);
[self.popoverController
presentPopoverFromRect:popoverRect
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
Alright!!! Thats all! If someOne needs help just let me know.
Best wishes!
override func viewDidAppear(_animated: Bool){
let navigationBar = self.navigationController?.navigationBar
navigationBar?.tintColor = UIColor(colorLiteralRed: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
the color of the button bar
navigationBar?.barStyle = UIBarStyle.black
the style bar
navigationBar?.barTintColor = UIColor(colorLiteralRed: 0.89, green: 0.55, blue: 0.69, alpha: 1.0)
the color of the bar
let imageView(frame: CGRect(x: 0, y: 0, width: 250, height: 80))
imageView.contentMode = .scaleAspectFit
}
image bar
all the code
override func viewDidAppear(_animated: Bool) {
let navigationBar = self.navigationController?.navigationBar
navigationBar?.tintColor = UIColor(colorLiteralRed: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
navigationBar?.barStyle = UIBarStyle.black
navigationBar?.barTintColor = UIColor(colorLiteralRed: 0.89, green: 0.55, blue: 0.69, alpha: 1.0)
let imageView(frame: CGRect(x: 0, y: 0, width: 250, height: 80))
imageView.contentMode = .scaleAspectFit
}

UITableView: `tableView:cellForRowAtIndexPath:` not called for all rows

I have created a UITableView with 4 Sections and 3 Rows so totally 12 Rows. But
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
The above method only gets called for 9 times instead of 12 times. Why this happening?
My 4th section is not getting constructed but my 1st section gets duplicated as 4th section.
Please refer my code below
#interface MainViewController : UITableViewController<UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource>
{
}
#end
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
CGRect frameRect = CGRectMake(0,0,320,460);
UITableView *tableView = [[UITableView alloc] initWithFrame:frameRect
style:UITableViewStyleGrouped];
tableView.delegate = self;
tableView.dataSource = self;
tableView.backgroundColor = [UIColor purpleColor];
tableView.scrollEnabled = YES;
self.view = tableView;
[tableView release];
[super viewDidLoad];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return 3;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 4;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"CELL IS NIL %i", indexPath.section);
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
if (indexPath.section == 0)
{
if(indexPath.row == 0)
{
cell.text = #"Tmail";
UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)];
aField.placeholder = #"Mandatory";
aField.delegate = self;
aField.textColor = [UIColor blackColor];
[cell addSubview:aField];
[aField release];
}
else if ( indexPath.row == 1 )
{
cell.text = #"English";
UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)];
aField.placeholder = #"Mandatory";
aField.delegate = self;
aField.textColor = [UIColor blackColor];
[cell addSubview:aField];
[aField release];
}
else
{
cell.text = #"Hindi";
UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)];
aField.placeholder = #"Mandatory";
aField.delegate = self;
aField.textColor = [UIColor blackColor];
[cell addSubview:aField];
[aField release];
}
}
else if (indexPath.section == 1)
{
if(indexPath.row == 0)
{
cell.text = #"Street";
UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)];
aField.placeholder = #"Mandatory";
aField.delegate = self;
aField.textColor = [UIColor blackColor];
[cell addSubview:aField];
[aField release];
}
else if ( indexPath.row == 1 )
{
cell.text = #"City";
UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)];
aField.placeholder = #"Mandatory";
aField.delegate = self;
aField.textColor = [UIColor blackColor];
[cell addSubview:aField];
[aField release];
}
else
{
cell.text = #"State";
UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)];
aField.placeholder = #"Mandatory";
aField.delegate = self;
aField.textColor = [UIColor blackColor];
[cell addSubview:aField];
[aField release];
}
}
else if (indexPath.section == 2)
{
if(indexPath.row == 0)
{
cell.text = #"Salem";
UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)];
aField.placeholder = #"Mandatory";
aField.delegate = self;
aField.textColor = [UIColor blackColor];
[cell addSubview:aField];
[aField release];
}
else if ( indexPath.row == 1 )
{
cell.text = #"Samalpatti";
UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)];
aField.placeholder = #"Mandatory";
aField.delegate = self;
aField.textColor = [UIColor blackColor];
[cell addSubview:aField];
[aField release];
}
else
{
cell.text = #"Chennai";
UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)];
aField.placeholder = #"Mandatory";
aField.delegate = self;
aField.textColor = [UIColor blackColor];
[cell addSubview:aField];
[aField release];
}
}
else if (indexPath.section == 3)
{
if(indexPath.row == 0)
{
cell.text = #"NOKIA";
UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)];
aField.placeholder = #"Mandatory";
aField.delegate = self;
aField.textColor = [UIColor blackColor];
[cell addSubview:aField];
[aField release];
}
else if ( indexPath.row == 1)
{
cell.text = #"SAMSUNG";
UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)];
aField.placeholder = #"Mandatory";
aField.delegate = self;
aField.textColor = [UIColor blackColor];
[cell addSubview:aField];
[aField release];
}
else
{
cell.text = #"SONY";
UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)];
aField.placeholder = #"Mandatory";
aField.delegate = self;
aField.textColor = [UIColor blackColor];
[cell addSubview:aField];
[aField release];
}
}
}
return cell;
}
actually when you scroll then other rows are created in case you having more rows,so think you need to write this code like this,
Edited one
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"CELL IS NIL %i", indexPath.section);
static NSString *CellIdentifier = #"Cell";
if (cell == nil)
{
CGRect CellFrame = CGRectMake(0, 0, 300, 70);
cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:CellIdentifier] autorelease];
CGRect labLabelFrame = CGRectMake(5, 5, 250, 40);
UILabel *lblTemp;
lblTemp = [[UILabel alloc] initWithFrame:labLabelFrame];
lblTemp.tag = 1;
[cell.contentView addSubview:lblTemp];
[lblTemp release];
CGRect textFieldFrame= CGRectMake(200, 5, 120, 30);
UITextField *txt=[[UITextField alloc] initWithFrame:textFieldFrame];
text.tag=2;
[cell.contentView addSubview:txt];
[txt release];
}
UILabel *cellLabel = (UILabel *)[cell viewWithTag:1];
UITextField *aField=(UITextField *)[cell viewWithTag:2];
aField.text=#"";
aField.placeholder=#"";
if (indexPath.section == 0)
{
if(indexPath.row == 0)
{
cellLabel.text = #"Tmail";
aField.placeholder = #"Mandatory";
aField.delegate = self;
aField.textColor = [UIColor blackColor];
}
else if ( indexPath.row == 1 )
{
cellLabel.text = #"English";
aField.placeholder = #"Mandatory";
aField.delegate = self;
aField.textColor = [UIColor blackColor];
}
else
{
cellLabel.text = #"Hindi";
aField.placeholder = #"Mandatory";
aField.delegate = self;
aField.textColor = [UIColor blackColor];
}
}
else if (indexPath.section == 1)
{
if(indexPath.row == 0)
{
cellLabel.text = #"Street";
aField.placeholder = #"Mandatory";
aField.delegate = self;
aField.textColor = [UIColor blackColor];
}
else if ( indexPath.row == 1 )
{
cellLabel.text = #"City";
aField.placeholder = #"Mandatory";
aField.delegate = self;
aField.textColor = [UIColor blackColor];
}
else
{
cellLabel.text = #"State";
aField.placeholder = #"Mandatory";
aField.delegate = self;
aField.textColor = [UIColor blackColor];
}
}
else if (indexPath.section == 2)
{
if(indexPath.row == 0)
{
cellLabel.text = #"Salem";
aField.placeholder = #"Mandatory";
aField.delegate = self;
aField.textColor = [UIColor blackColor];
}
else if ( indexPath.row == 1 )
{
cellLabel.text = #"Samalpatti";
aField.placeholder = #"Mandatory";
aField.delegate = self;
aField.textColor = [UIColor blackColor];
}
else
{
cellLabel.text = #"Chennai";
aField.placeholder = #"Mandatory";
aField.delegate = self;
aField.textColor = [UIColor blackColor];
}
}
else if (indexPath.section == 3)
{
if(indexPath.row == 0)
{
cellLabel.text = #"NOKIA";
aField.placeholder = #"Mandatory";
aField.delegate = self;
aField.textColor = [UIColor blackColor];
}
else if ( indexPath.row == 1)
{
cellLabel.text = #"SAMSUNG";
aField.placeholder = #"Mandatory";
aField.delegate = self;
aField.textColor = [UIColor blackColor];
}
else
{
cellLabel.text = #"SONY";
aField.placeholder = #"Mandatory";
aField.delegate = self;
aField.textColor = [UIColor blackColor];
}
}
return cell;
}
Problem with your code is:
1) Cell is being reused. but the textfield what you added as subview is not removed!
2) Whenever your cellforRowAtIndex gets called, you are adding textfield each time. But you forgot that you have already added one textfield but haven't removed it.
This is the reason for overlapping.
Solution:
Subclass the UITableViewCell and add textfield in its initWithFrame method and pass the data to that cell. This will solve your problem and also this will reduce your cellForRowAtIndex method length.
Sample code:
CustomCell.h:
#import <UIKit/UIKit.h>
#class contactr;
#interface CustomTableCell : UITableViewCell
{
UITextField *mTextField;
}
#property(nonatomic , retain) UITextField *textField;
#end
CustomCell.m:
#import "CustomTableCell.h"
#synthesize textField = mTextField;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)];
self.textField = textField;
[textField release];
[self addSubview:self.textField];
}
return self;
}
- (void)dealloc
{
self.textfield = nil;
[super dealloc];
}
#end
Use this class as your tableViewCell.
And your cellForRowAtIndex must be:
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"CELL IS NIL %i", indexPath.section);
static NSString *CellIdentifier = #"Cell";
CustomTableCell *cell = (CustomTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[CustomTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textField.placeholder = #"";
cell.textField.text = #"";
if (indexPath.section == 0)
{
if(indexPath.row == 0)
{
cell.text = #"Tmail";
cell.textField.placeholder = #"Mandatory";
cell.textField.delegate = self;
}
else if ( indexPath.row == 1 )
{
cell.text = #"English";
cell.textField.placeholder = #"Mandatory";
cell.textField.delegate = self;
}
else
{
cell.text = #"Hindi";
cell.textField.placeholder = #"Mandatory";
cell.textField.delegate = self;
}
}
else if (indexPath.section == 1)
{
if(indexPath.row == 0)
{
cell.text = #"Street";
cell.textField.placeholder = #"Mandatory";
cell.textField.delegate = self;
}
else if ( indexPath.row == 1 )
{
cell.text = #"City";
cell.textField.placeholder = #"Mandatory";
cell.textField.delegate = self;
}
else
{
cell.text = #"State";
cell.textField.placeholder = #"Mandatory";
cell.textField.delegate = self;
}
}
else if (indexPath.section == 2)
{
if(indexPath.row == 0)
{
cell.text = #"Salem";
cell.textField.placeholder = #"Mandatory";
cell.textField.delegate = self;
}
else if ( indexPath.row == 1 )
{
cell.text = #"Samalpatti";
cell.textField.placeholder = #"Mandatory";
cell.textField.delegate = self;
cell.textField.textColor = [UIColor blackColor];
}
else
{
cell.text = #"Chennai";
cell.textField.placeholder = #"Mandatory";
cell.textField.delegate = self;
cell.textField.textColor = [UIColor blackColor];
}
}
else if (indexPath.section == 3)
{
if(indexPath.row == 0)
{
cell.text = #"NOKIA";
cell.textField.placeholder = #"Mandatory";
cell.textField.delegate = self;
cell.textField.textColor = [UIColor blackColor];
}
else if ( indexPath.row == 1)
{
cell.text = #"SAMSUNG";
cell.textField.placeholder = #"Mandatory";
cell.textField.delegate = self;
cell.textField.textColor = [UIColor blackColor];
}
else
{
cell.text = #"SONY";
cell.textField.placeholder = #"Mandatory";
cell.textField.delegate = self;
cell.textField.textColor = [UIColor blackColor];
}
}
return cell;
}
From your initial code, the tableview is behaving normally.
You create cells for the first 3 sections because they are visible and there are no extra cell in the table view which you can reuse.
But, whenever you get scroll to the 4th section (which is not visible at first), you start reusing cells (which happen to be the ones from the 1st section that is not out of the screen).
In order not to lose the data the user has entered in each cell you need to store each UITextField(or at least the text from it) in a separate array. When the table needs a certain cell, you look for the textfield in your array. If it's not there, create and add a new UITextField.
PS: I hope the code doesn't have any typos. I'm at work and I don't have a mac here.
#interface MainViewController : UITableViewController
{
NSMutableArray* textFields;
}
#end
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
CGRect frameRect = CGRectMake(0,0,320,460);
UITableView *tableView = [[UITableView alloc] initWithFrame:frameRect
style:UITableViewStyleGrouped];
tableView.delegate = self;
tableView.dataSource = self;
tableView.backgroundColor = [UIColor purpleColor];
tableView.scrollEnabled = YES;
self.view = tableView;
[tableView release];
[super viewDidLoad];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return 3;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 4;
}
- (UITextField*)getTextFieldAtIndexPath:(NSIndexPath*)indexPath
{
if (textFields == nil)
textFields = [[NSMutableArray alloc] init];
// Add padding, just in case UITableView doesn't need the cells in order
while (indexPath.section >= [textFields count])
{
[textFields addObject:[NSNull null]];
}
// Get the section list
NSMutableArray* sectionList = [textFields objectAtIndex:indexPath.section];
if (sectionList == [NSNull null])
{
sectionList = [[[NSMutableArray alloc] init] autorelease];
[textFields replaceObjectAtIndex:indexPath.section withObject:sectionList];
}
// Now that we have the section list, we need to do the same for the row
while (indexPath.row >= [sectionList count])
{
[sectionList addObject:[NSNull null]];
}
UITextField* afield = [sectionList objectAtIndex:indexPath.row];
if (afield == [NSNull null])
{
// If there was no afield stored there, just create a new one
afield = [[UITextField alloc] initWithFrame:CGRectMake(100,10,200,40)];
afield.placeholder = #"Mandatory";
afield.delegate = self;
afield.textColor = [UIColor blackColor];
[sectionList replaceObjectAtIndex:indexPath.row withObject:aField];
[afield release];
}
return afield;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"CELL IS NIL %i", indexPath.section);
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UITextField *aField;
// If we can't reuse the cell, just create a new one
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
else
{
// if we are reusing the cell, just remove the old textField to make room for the new one
[[cell.contentView viewForTag:1234] removeFromSuperview];
}
// Get the text field for this cell. The method below will create a new one if it doesn't exist, or just return the existing one if found.
aField = [self getTextFieldAtIndexPath:indexPath];
// Just add this textfield to the cell
[cell.contentView addSubView:aField];
// Now that we added the textfield, we only need to set the title of the cell. The content of the textField will be cached and moved around.
if (indexPath.section == 0)
{
if(indexPath.row == 0)
cell.text = #"Tmail";
else if ( indexPath.row == 1 )
cell.text = #"English";
else
cell.text = #"Hindi";
}
else if (indexPath.section == 1)
{
if(indexPath.row == 0)
cell.text = #"Street";
else if ( indexPath.row == 1 )
cell.text = #"City";
else
cell.text = #"State";
}
else if (indexPath.section == 2)
{
if(indexPath.row == 0)
cell.text = #"Salem";
else if ( indexPath.row == 1 )
cell.text = #"Samalpatti";
else
cell.text = #"Chennai";
}
else if (indexPath.section == 3)
{
if(indexPath.row == 0)
cell.text = #"NOKIA";
else if ( indexPath.row == 1)
cell.text = #"SAMSUNG";
else
cell.text = #"SONY";
}
}
return cell;
}

Resources